Move graph data and nav html inline to template

This commit is contained in:
Brian Read 2024-07-14 08:20:09 +01:00
parent a9be56deae
commit aa0ebc76e9
3 changed files with 141 additions and 44 deletions

View File

@ -1,3 +1,4 @@
<div class="${title}">
<h2>${title}</h2>
<table style="border-collapse:collapse;">
<thead>
@ -12,4 +13,5 @@
</tbody>
</table>
</div>

View File

@ -1,7 +1,4 @@
<!DOCTYPE html>
<!-- 1. Take out style for table for email
2. Switch "see in Browser" to "index" in web version
-->
<html><head>
<meta charset="utf-8">
<title>SMEServer Mailstats</title>
@ -41,12 +38,31 @@
<body>
<div style="width:100%;overflow-x:auto;font-size:1cqw">
<!---Navigation here-->
<div class='linksattop'>
<a class='prevlink' href='http://${DomainName}/mailstats/mailstats_for_${PreviousDate}.html'>Previous</a>
<div class='divshowindex'><a class='showindex' href='http://${DomainName}/mailstats/'>Index of files</a></div>
<a class='nextlink' href='http://${DomainName}/mailstats/mailstats_for_${NextDate}.html'>Next</a>
</div>
<br />
<h2>${title}</h2>
<br />
<div class="headerpanel">
<div class = "innerheaderpanel">
<!---Add in header information here -->
</div>
</div>
<br />
<br />
<!--Tabs -->
<div class="tab-container">
<div class="tab tab-active" onclick="openTab(event, 'tab1')">Table</div>
<div class="tab" onclick="openTab(event, 'tab2')">Stacked Bar Graph</div>
<div class="tab" onclick="openTab(event, 'tab3')">Heat Map</div>
<div class="tab" onclick="openTab(event, 'tab4')">Line Graph</div>
</div>
<div id="tab1" class="tab-content tab-content-active">
<div class = "maintable">
<table style="border-collapse:collapse;width:98%">
<thead>
<tr>
@ -85,11 +101,63 @@
</tr>
</tbody>
</table>
<!---Add in sub tables here -->
<br />
<footer>${version}</footer>
</div>
</div>
<!-- Next Tab-->
<div id="tab2" class="tab-content">
<!--<div class = "parent-container">
<iframe class="iframe-container" src="stacked_bar_2024-07-12.html" title="Embedded Page">
Your browser does not support iframes.
</iframe>
</div>
-->
<a href="stacked_bar_2024-07-12.html">${structure: stacked_bar_graph}</a>
<a href="stacked_bar_${reporting_date}.html">${structure: stacked_bar_graph}</a>
</div>
<!-- Next Tab-->
<div id="tab3" class="tab-content">
<!--
<div class = "parent-container">
<iframe class="iframe-container" src="heatmap_2024-07-12.html" title="Embedded Page">
Your browser does not support iframes.
</iframe>
</div>
-->
<a href="heatmap_${reporting_date}.html">${structure: heatmap}</a>
</div>
<!-- Next Tab-->
<div id="tab4" class="tab-content">
<!--<div class = "parent-container">
<iframe class="iframe-container" src="line_graph_2024-07-12.html" title="Embedded Page">
Your browser does not support iframes.
</iframe>
</div>
-->
<a href="line_graph_${reporting_date}.html">${structure: line_graph}</a>
</div>
<script>window.onload = function(){doNavs();} </script>
<div class = "subtables">
<!---Add in sub tables here -->
</div>
<br />
<footer class="footer">${version}</footer>
<script>
window.onload = function(){doNavs();}
function openTab(event, tabId){
// Get all elements with class="tab-content" and hide them
const tabContents = document.querySelectorAll('.tab-content');
tabContents.forEach(content => content.classList.remove('tab-content-active'));
// Get all elements with class="tab" and remove the class "tab-active"
const tabs = document.querySelectorAll('.tab');
tabs.forEach(tab => tab.classList.remove('tab-active'));
// Show the current tab content, and add an "active" class to the clicked tab
document.getElementById(tabId).classList.add('tab-content-active');
event.target.classList.add('tab-active');}
</script>
<p class="cssvalid">
<a href="http://jigsaw.w3.org/css-validator/check/referer">

View File

@ -231,12 +231,21 @@ def create_stacked_bar_graph(data2d, xLabels, yLabels, save_path='stacked_bar_gr
title='Stacked Bar Graph by Hour',
xaxis=dict(title='Hour'),
yaxis=dict(title='Values'),
legend_title_text='Categories'
legend_title_text='Categories',
margin = {
'l': 50, #left margin
'r': 120, #right margin
't': 50, #top margin
'b': 50 #bottom margin
}
)
# Save the graph to an HTML file
fig.write_html(save_path)
# Write it to a var and return the string
graph_html = fig.to_html(full_html=False)
return graph_html
def sanitize_and_filter_data(data2d, exclude_labels, xLabels):
"""
@ -321,6 +330,10 @@ def create_heatmap(data2d, xLabels, yLabels, save_path='heatmap.html'):
fig.update_yaxes(showticklabels=True, showline=True, linewidth=2, linecolor='black', mirror=True)
fig.write_html(save_path)
# Write it to a var and return the string
graph_html = fig.to_html(full_html=False)
return graph_html
def create_line_chart(data2d, xLabels,yLabels, save_path='line_chart.html'):
fig = go.Figure()
@ -354,6 +367,9 @@ def create_line_chart(data2d, xLabels,yLabels, save_path='line_chart.html'):
)
fig.write_html(save_path)
# Write it to a var and return the string
graph_html = fig.to_html(full_html=False)
return graph_html
def save_summaries_to_db(date_str, hour, parsed_data):
@ -1357,6 +1373,25 @@ if __name__ == "__main__":
#print(columnCounts_2d)
#quit()
#Compute next and previous dates
day_format = "%Y-%m-%d"
# Convert the time string to a datetime object
date_obj = datetime.strptime(analysis_date, day_format)
# Compute the next date by adding one day
next_date = date_obj + timedelta(days=1)
# Compute the previous date by subtracting one day
previous_date = date_obj - timedelta(days=1)
# Convert the datetime objects back to strings in the desired format
next_date_str = next_date.strftime(day_format)
previous_date_str = previous_date.strftime(day_format)
# Create graphs of data
yLabels = [f'{i:02d}:00' for i in range(len(columnCounts_2d))]
stacked_Bar_html = create_stacked_bar_graph(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'stacked_bar_'+analysis_date+'.html')
heatmap_html = create_heatmap(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'heatmap_'+analysis_date+'.html')
line_graph_html = create_line_chart(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'line_graph_'+analysis_date+'.html')
#Now apply the results to the chameleon template - main table
# Path to the template file
template_path = template_dir+'mailstats.html.pt'
@ -1371,7 +1406,14 @@ if __name__ == "__main__":
rendered_html = template(array_2d=columnCounts_2d, column_headers=columnHeaders,
reporting_date=analysis_date, title=hello_string,
version=version_string,
nolinks=nolinks)
nolinks=nolinks,
stacked_bar_graph=stacked_Bar_html,
heatmap=heatmap_html,
line_graph=line_graph_html,
PreviousDate=previous_date_str,
NextDate=next_date_str,
DomainName=DomainName
)
except Exception as e:
print(f"Chameleon template Exception {e}")
except Exception as e:
@ -1403,32 +1445,24 @@ if __name__ == "__main__":
cursor.close()
conn.close()
#Add in navigation html - next/previous/see in browser
day_format = "%Y-%m-%d"
# Convert the time string to a datetime object
date_obj = datetime.strptime(analysis_date, day_format)
# Compute the next date by adding one day
next_date = date_obj + timedelta(days=1)
# Compute the previous date by subtracting one day
previous_date = date_obj - timedelta(days=1)
# Convert the datetime objects back to strings in the desired format
next_date_str = next_date.strftime(day_format)
previous_date_str = previous_date.strftime(day_format)
navigation_str_html = "<div class='linksattop'>\
<a class='prevlink' href='http://${DomainName}/mailstats/mailstats_for_${PreviousDate}.html'>Previous</a>\
<div class='divshowindex'><a class='showindex' href='http://${DomainName}/mailstats/'>Index of files</a></div>\
<a class='nextlink' href='http://${DomainName}/mailstats/mailstats_for_${NextDate}.html'>Next</a>\
</div>"
try:
template = PageTemplate(navigation_str_html)
try:
Nav_str = template(PreviousDate=previous_date_str,NextDate=next_date_str,TodayDate=analysis_date,DomainName=DomainName)
except Exception as e:
print(f"Chameleon nav template Exception {e}")
except Exception as e:
print(f"Chameleon nav render Exception {e}")
# And insert it
total_html = insert_string_after(total_html,Nav_str, "<!---Navigation here-->")
# #Add in navigation html - next/previous/see in browser
# navigation_str_html = "<div class='linksattop'>\
# <a class='prevlink' href='http://${DomainName}/mailstats/mailstats_for_${PreviousDate}.html'>Previous</a>\
# <div class='divshowindex'><a class='showindex' href='http://${DomainName}/mailstats/'>Index of files</a></div>\
# <a class='nextlink' href='http://${DomainName}/mailstats/mailstats_for_${NextDate}.html'>Next</a>\
# </div>"
# try:
# template = PageTemplate(navigation_str_html)
# try:
# Nav_str = template(PreviousDate=previous_date_str,NextDate=next_date_str,TodayDate=analysis_date,DomainName=DomainName)
# except Exception as e:
# print(f"Chameleon nav template Exception {e}")
# except Exception as e:
# print(f"Chameleon nav render Exception {e}")
# # And insert it
# total_html = insert_string_after(total_html,Nav_str, "<!---Navigation here-->")
# Write the rendered HTML to a file
output_path = html_page_dir+'mailstats_for_'+analysis_date
@ -1451,13 +1485,6 @@ if __name__ == "__main__":
else:
text_file_path = ""
# Create graphs of data
#yLabels = [f'Hour {i}' for i in range(len(columnCounts_2d))]
yLabels = [f'{i:02d}:00' for i in range(len(columnCounts_2d))]
create_stacked_bar_graph(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'stacked_bar_'+analysis_date+'.html')
#yLabels = [f'Hour {i}' for i in range(26)]
create_heatmap(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'heatmap_'+analysis_date+'.html')
create_line_chart(columnCounts_2d,columnHeaders,yLabels,html_page_dir+'line_graph_'+analysis_date+'.html')
html_content = None
text_content = None