I broke it.

This commit is contained in:
mattbk
2017-11-16 02:45:13 +00:00
parent fbaa1beb24
commit 59504da8e4
2 changed files with 72 additions and 80 deletions

View File

@ -50,6 +50,7 @@ import datetime
import arrow import arrow
import json import json
import plotly import plotly
import sqlite3
from plotly.graph_objs import * from plotly.graph_objs import *
app = Flask(__name__) app = Flask(__name__)
@ -83,62 +84,61 @@ def history():
print "rendering history.html with: %s, %s, %s" % (timezone, from_date_str, to_date_str) print "rendering history.html with: %s, %s, %s" % (timezone, from_date_str, to_date_str)
# Create new record tables so that datetimes are adjusted back to the user browser's time zone. # Create new record tables so that datetimes are adjusted back to the user browser's time zone.
time_series_adjusted_tempreratures = [] # time_series_adjusted_temperatures = []
time_series_adjusted_humidities = [] # time_series_adjusted_humidities = []
time_series_temprerature_values = [] # time_series_temperature_values = []
time_series_humidity_values = [] # time_series_humidity_values = []
#
for record in temperatures: # for record in temperatures:
local_timedate = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone) # local_timedate_series = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone)
time_series_adjusted_tempreratures.append(local_timedate.format('YYYY-MM-DD HH:mm')) # time_series_adjusted_temperatures.append(local_timedate_series.format('YYYY-MM-DD HH:mm'))
time_series_temprerature_values.append(round(record[2],2)) # time_series_temperature_values.append(round(record[2],2))
#
for record in humidities: # for record in humidities:
local_timedate = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone) # local_timedate_series = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone)
time_series_adjusted_humidities.append(local_timedate.format('YYYY-MM-DD HH:mm')) #Best to pass datetime in text # time_series_adjusted_humidities.append(local_timedate_series.format('YYYY-MM-DD HH:mm')) #Best to pass datetime in text
#so that Plotly respects it # so that Plotly respects it
time_series_humidity_values.append(round(record[2],2)) # time_series_humidity_values.append(round(record[2],2))
#
#
temp = Scatter( # temp = Scatter(
x=time_series_adjusted_tempreratures, # x=time_series_adjusted_temperatures,
y=time_series_temprerature_values, # y=time_series_temperature_values,
name='Temperature' # name='Temperature'
) # )
hum = Scatter( # hum = Scatter(
x=time_series_adjusted_humidities, # x=time_series_adjusted_humidities,
y=time_series_humidity_values, # y=time_series_humidity_values,
name='Humidity', # name='Humidity',
yaxis='y2' # yaxis='y2'
) # )
#
data = Data([temp, hum]) # data = Data([temp, hum])
#
layout = Layout( # layout = Layout(
title="Temperature and Humidity in Clayton's Apartment", # title="Temperature and Humidity",
xaxis=XAxis( # xaxis=XAxis(
type='date', # type='date',
autorange=True # autorange=True
), # ),
yaxis=YAxis( # yaxis=YAxis(
title='Fahrenheit', # title='Fahrenheit',
type='linear', # type='linear',
autorange=True # autorange=True
), # ),
yaxis2=YAxis( # yaxis2=YAxis(
title='Percent', # title='Percent',
type='linear', # type='linear',
autorange=True, # autorange=True,
overlaying='y', # overlaying='y',
side='right' # side='right'
) # )
#
) # )
#
fig = Figure(data=data, layout=layout) # fig = Figure(data=data, layout=layout)
#plot_url = py.plot(fig, filename='lab_temp_hum') # graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template("history.html", timezone = timezone, return render_template("history.html", timezone = timezone,
temp = time_adjusted_temperatures, temp = time_adjusted_temperatures,
@ -149,10 +149,10 @@ def history():
query_string = request.query_string, #This query string is used query_string = request.query_string, #This query string is used
#by the Plotly link #by the Plotly link
hum_items = len(humidities), hum_items = len(humidities),
graphJSON=graphJSON) #graphJSON=graphJSON,
)
def get_records(): def get_records():
import sqlite3
from_date_str = request.args.get('from',time.strftime("%Y-%m-%d 00:00")) #Get the from date value from the URL from_date_str = request.args.get('from',time.strftime("%Y-%m-%d 00:00")) #Get the from date value from the URL
to_date_str = request.args.get('to',time.strftime("%Y-%m-%d %H:%M")) #Get the to date value from the URL to_date_str = request.args.get('to',time.strftime("%Y-%m-%d %H:%M")) #Get the to date value from the URL
timezone = request.args.get('timezone','Etc/UTC'); timezone = request.args.get('timezone','Etc/UTC');

View File

@ -151,6 +151,7 @@
jQuery("#range_select").submit(); jQuery("#range_select").submit();
}); });
jQuery("#plotly").click(function(){ jQuery("#plotly").click(function(){
jQuery("#plotly_wait").text("Sending data..."); jQuery("#plotly_wait").text("Sending data...");
jQuery("#plotly_url").text(""); jQuery("#plotly_url").text("");
@ -164,16 +165,15 @@
}); });
return false; //This is so that the click on the link does not cause the page to refresh return false; //This is so that the click on the link does not cause the page to refresh
}); });
</script> </script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script> <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<script> <script>
google.load('visualization', '1', {packages: ['corechart']}); google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart); google.setOnLoadCallback(drawChart);
function drawChart() { function drawChart() {
var data = new google.visualization.DataTable(); var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time'); data.addColumn('datetime', 'Time');
data.addColumn('number', 'Temperature'); data.addColumn('number', 'Temperature');
@ -182,7 +182,6 @@
[new Date({{row[0][0:4]}},{{row[0][5:7]}}-1,{{row[0][8:10]}},{{row[0][11:13]}},{{row[0][14:16]}}),{{'%0.2f'|format(row[1])}}], [new Date({{row[0][0:4]}},{{row[0][5:7]}}-1,{{row[0][8:10]}},{{row[0][11:13]}},{{row[0][14:16]}}),{{'%0.2f'|format(row[1])}}],
{% endfor %} {% endfor %}
]); ]);
var options = { var options = {
width: 600, width: 600,
height: 563, height: 563,
@ -196,21 +195,15 @@
title: 'Temperature', title: 'Temperature',
curveType: 'function' //Makes line curved curveType: 'function' //Makes line curved
}; };
var chart = new google.visualization.LineChart(document.getElementById('chart_temps')); var chart = new google.visualization.LineChart(document.getElementById('chart_temps'));
chart.draw(data, options); chart.draw(data, options);
} }
</script> </script>
<script> <script>
google.load('visualization', '1', {packages: ['corechart']}); google.load('visualization', '1', {packages: ['corechart']});
google.setOnLoadCallback(drawChart); google.setOnLoadCallback(drawChart);
function drawChart() { function drawChart() {
var data = new google.visualization.DataTable(); var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time'); data.addColumn('datetime', 'Time');
data.addColumn('number', 'Humidity'); data.addColumn('number', 'Humidity');
@ -219,7 +212,6 @@
[new Date({{row[0][0:4]}},{{row[0][5:7]}}-1,{{row[0][8:10]}},{{row[0][11:13]}},{{row[0][14:16]}}),{{'%0.2f'|format(row[1])}}], [new Date({{row[0][0:4]}},{{row[0][5:7]}}-1,{{row[0][8:10]}},{{row[0][11:13]}},{{row[0][14:16]}}),{{'%0.2f'|format(row[1])}}],
{% endfor %} {% endfor %}
]); ]);
var options = { var options = {
width: 600, width: 600,
height: 563, height: 563,
@ -233,29 +225,29 @@
title: 'Humidity', title: 'Humidity',
curveType: 'function' //Makes line curved curveType: 'function' //Makes line curved
}; };
var chart = new google.visualization.LineChart(document.getElementById('chart_humid')); var chart = new google.visualization.LineChart(document.getElementById('chart_humid'));
chart.draw(data, options); chart.draw(data, options);
} }
</script> </script>
<!--
<div id='plotly-plot'></div> <div id='plotly-plot'></div>
{{ graphJSON }}
<footer> <footer>
<!-- D3.js https://github.com/d3/d3/releases--> <!~~ D3.js https://github.com/d3/d3/releases~~>
<script src="static/javascript/d3.min.js"></script> <script src="static/javascript/d3.min.js"></script>
<!-- jQuery https://jquery.com/download/ --> <!~~ jQuery https://jquery.com/download/ ~~>
<script src="static/javascript/jquery-3.2.1.min.js"></script> <script src="static/javascript/jquery-3.2.1.min.js"></script>
<!-- Plotly.js https://github.com/plotly/plotly.js/releases/ --> <!~~ Plotly.js https://github.com/plotly/plotly.js/releases/ ~~>
<script src="static/javascript/plotly.js"></script> <script src="static/javascript/plotly.js"></script>
<!--Plot Scaled-D on results.html--> <!~~Plot data~~>
<script type="text/javascript"> <script type="text/javascript">
var graph = {{graphJSON | safe}}; var graph = {{graphJSON | safe}};
Plotly.plot('plotly-plot', graph.data, graph.layout); Plotly.plot('plotly-plot', graph.data, graph.layout);
</script> </script>
</footer> </footer>
-->
</html> </html>