Speed improvements: Python #26
89
pi_temp.py
89
pi_temp.py
@ -71,19 +71,6 @@ def lab_temp():
|
||||
def history():
|
||||
temperatures, humidities, timezone, from_date_str, to_date_str = get_records()
|
||||
|
||||
# Create new record tables so that datetimes are adjusted back to the user browser's time zone.
|
||||
time_adjusted_temperatures = []
|
||||
time_adjusted_humidities = []
|
||||
for record in temperatures:
|
||||
local_timedate = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone)
|
||||
time_adjusted_temperatures.append([local_timedate.format('YYYY-MM-DD HH:mm'), round(record[2],2)])
|
||||
|
||||
for record in humidities:
|
||||
local_timedate = arrow.get(record[0], "YYYY-MM-DD HH:mm").to(timezone)
|
||||
time_adjusted_humidities.append([local_timedate.format('YYYY-MM-DD HH:mm'), round(record[2],2)])
|
||||
|
||||
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.
|
||||
time_series_adjusted_temperatures = []
|
||||
time_series_adjusted_humidities = []
|
||||
@ -134,17 +121,9 @@ def history():
|
||||
fig = Figure(data=data, layout=layout)
|
||||
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
|
||||
|
||||
return render_template("history.html", timezone = timezone,
|
||||
temp = time_adjusted_temperatures,
|
||||
hum = time_adjusted_humidities,
|
||||
from_date = from_date_str,
|
||||
to_date = to_date_str,
|
||||
temp_items = len(temperatures),
|
||||
query_string = request.query_string, #This query string is used
|
||||
#by the Plotly link
|
||||
hum_items = len(humidities),
|
||||
graphJSON=graphJSON,
|
||||
)
|
||||
return render_template("history.html", timezone = timezone,
|
||||
graphJSON = graphJSON,
|
||||
)
|
||||
|
||||
def get_records():
|
||||
from_date_str = request.args.get('from',time.strftime("%Y-%m-%d 00:00")) #Get the from date value from the URL
|
||||
@ -196,68 +175,6 @@ def get_records():
|
||||
|
||||
return [temperatures, humidities, timezone, from_date_str, to_date_str]
|
||||
|
||||
@app.route("/to_plotly", methods=['GET']) #This method will send the data to ploty.
|
||||
def to_plotly():
|
||||
temperatures, humidities, timezone, from_date_str, to_date_str = get_records()
|
||||
|
||||
# Create new record tables so that datetimes are adjusted back to the user browser's time zone.
|
||||
time_series_adjusted_tempreratures = []
|
||||
time_series_adjusted_humidities = []
|
||||
time_series_temprerature_values = []
|
||||
time_series_humidity_values = []
|
||||
|
||||
for record in temperatures:
|
||||
local_timedate = 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_temprerature_values.append(round(record[2],2))
|
||||
|
||||
for record in humidities:
|
||||
local_timedate = 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
|
||||
#so that Plotly respects it
|
||||
time_series_humidity_values.append(round(record[2],2))
|
||||
|
||||
temp = Scatter(
|
||||
x=time_series_adjusted_tempreratures,
|
||||
y=time_series_temprerature_values,
|
||||
name='Temperature'
|
||||
)
|
||||
hum = Scatter(
|
||||
x=time_series_adjusted_humidities,
|
||||
y=time_series_humidity_values,
|
||||
name='Humidity',
|
||||
yaxis='y2'
|
||||
)
|
||||
|
||||
data = Data([temp, hum])
|
||||
|
||||
layout = Layout(
|
||||
title="Temperature and Humidity in Clayton's Apartment",
|
||||
xaxis=XAxis(
|
||||
type='date',
|
||||
autorange=True
|
||||
),
|
||||
yaxis=YAxis(
|
||||
title='Fahrenheit',
|
||||
type='linear',
|
||||
autorange=True
|
||||
),
|
||||
yaxis2=YAxis(
|
||||
title='Percent',
|
||||
type='linear',
|
||||
autorange=True,
|
||||
overlaying='y',
|
||||
side='right'
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
fig = Figure(data=data, layout=layout)
|
||||
#plot_url = py.plot(fig, filename='lab_temp_hum')
|
||||
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
|
||||
|
||||
return graphJSON
|
||||
|
||||
def validate_date(d):
|
||||
try:
|
||||
datetime.datetime.strptime(d, '%Y-%m-%d %H:%M')
|
||||
|
80
static/javascript/plotly-latest.min.js
vendored
Normal file
80
static/javascript/plotly-latest.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
186991
static/javascript/plotly.js
186991
static/javascript/plotly.js
File diff suppressed because one or more lines are too long
@ -26,8 +26,9 @@
|
||||
<!~~ jQuery https://jquery.com/download/ ~~>
|
||||
<script src="static/javascript/jquery-3.2.1.min.js"></script>
|
||||
<!~~ Plotly.js https://github.com/plotly/plotly.js/releases/ ~~>
|
||||
<script src="static/javascript/plotly.js"></script>
|
||||
<script src="static/javascript/plotly-latest.min.js"></script>
|
||||
<script src="static/javascript/jquery.datetimepicker.full.js"></script>
|
||||
<!~~ Apparently needed for range_h radio buttons? ~~>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js" ></script>
|
||||
</head>
|
||||
<body>
|
||||
@ -77,61 +78,17 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class='row' id='plotly-plot'></div>
|
||||
<div class="row">
|
||||
<div class="one-third column" style="margin-top: 5%">
|
||||
<strong>Showing all records</strong>
|
||||
<h2>Temperatures</h2>
|
||||
<table class="u-full-width">
|
||||
<thead style="display:block;">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th> °F</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody style="height:450px; overflow:scroll; display:block;">
|
||||
{% for row in temp %}
|
||||
<tr>
|
||||
<td>{{row[0]}}</td>
|
||||
<td>{{'%0.2f'|format(row[1])}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
<h2>Humidities</h2>
|
||||
<table class="u-full-width">
|
||||
<thead style="display:block;">
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th> %</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody style="height:450px; overflow:scroll; display:block;">
|
||||
{% for row in hum %}
|
||||
<tr>
|
||||
<td>{{row[0]}}</td>
|
||||
<td>{{'%0.2f'|format(row[1])}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="two-thirds column" style="margin-top: 5%">
|
||||
<div class="row">
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div id="chart_temps"></div>
|
||||
<div id="chart_humid"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<footer>
|
||||
<!~~Plot data~~>
|
||||
<script type="text/javascript">
|
||||
var graph = {{graphJSON | safe}};
|
||||
Plotly.plot('plotly-plot', graph.data, graph.layout);
|
||||
</script>
|
||||
|
||||
|
||||
<script>
|
||||
<script>
|
||||
jQuery( "#datetime_range" ).submit(function( event ) {
|
||||
timezone = jstz.determine();
|
||||
jQuery(".timezone").val(timezone.name());
|
||||
@ -153,15 +110,6 @@
|
||||
jQuery("#range_select").submit();
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
<footer>
|
||||
<!~~Plot data~~>
|
||||
<script type="text/javascript">
|
||||
var graph = {{graphJSON | safe}};
|
||||
Plotly.plot('plotly-plot', graph.data, graph.layout);
|
||||
</script>
|
||||
</footer>
|
||||
|
||||
-</html>
|
Reference in New Issue
Block a user