Cut out a bunch to speed up plot.

This commit is contained in:
mattbk
2017-11-17 04:01:24 +00:00
parent baa3520af6
commit 3210dd89a2
4 changed files with 95 additions and 187141 deletions

View File

@ -70,19 +70,6 @@ def lab_temp():
@app.route("/history", methods=['GET']) #Add date limits in the URL #Arguments: from=2015-03-04&to=2015-03-05 @app.route("/history", methods=['GET']) #Add date limits in the URL #Arguments: from=2015-03-04&to=2015-03-05
def history(): def history():
temperatures, humidities, timezone, from_date_str, to_date_str = get_records() 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. # Create new record tables so that datetimes are adjusted back to the user browser's time zone.
time_series_adjusted_temperatures = [] time_series_adjusted_temperatures = []
@ -134,17 +121,9 @@ def history():
fig = Figure(data=data, layout=layout) fig = Figure(data=data, layout=layout)
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, graphJSON = graphJSON,
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,
)
def get_records(): 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 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] 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): def validate_date(d):
try: try:
datetime.datetime.strptime(d, '%Y-%m-%d %H:%M') datetime.datetime.strptime(d, '%Y-%m-%d %H:%M')

80
static/javascript/plotly-latest.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -26,8 +26,9 @@
<!~~ 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-latest.min.js"></script>
<script src="static/javascript/jquery.datetimepicker.full.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> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js" ></script>
</head> </head>
<body> <body>
@ -77,61 +78,17 @@
</div> </div>
</div> </div>
<div class='row' id='plotly-plot'></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>                              &deg;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>
</body>
<script> <footer>
<!~~Plot data~~>
<script type="text/javascript">
var graph = {{graphJSON | safe}};
Plotly.plot('plotly-plot', graph.data, graph.layout);
</script>
<script>
jQuery( "#datetime_range" ).submit(function( event ) { jQuery( "#datetime_range" ).submit(function( event ) {
timezone = jstz.determine(); timezone = jstz.determine();
jQuery(".timezone").val(timezone.name()); jQuery(".timezone").val(timezone.name());
@ -153,15 +110,6 @@
jQuery("#range_select").submit(); jQuery("#range_select").submit();
}); });
</script> </script>
<footer>
<!~~Plot data~~>
<script type="text/javascript">
var graph = {{graphJSON | safe}};
Plotly.plot('plotly-plot', graph.data, graph.layout);
</script>
</footer> </footer>
-</html> -</html>