83
									
								
								pi_temp.py
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								pi_temp.py
									
									
									
									
									
								
							@@ -71,19 +71,6 @@ def lab_temp():
 | 
				
			|||||||
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  = []
 | 
				
			||||||
	time_series_adjusted_humidities 	= []
 | 
						time_series_adjusted_humidities 	= []
 | 
				
			||||||
@@ -135,14 +122,6 @@ def history():
 | 
				
			|||||||
	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,
 | 
					 | 
				
			||||||
												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,
 | 
																graphJSON = graphJSON,
 | 
				
			||||||
											)
 | 
																)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -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
									
								
							
							
						
						
									
										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/ ~~>
 | 
					    <!~~ 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,59 +78,15 @@
 | 
				
			|||||||
      </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>                              °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>
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
 
 | 
					  <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 ) {
 | 
					    jQuery( "#datetime_range" ).submit(function( event ) {
 | 
				
			||||||
@@ -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>      
 | 
				
			||||||
		Reference in New Issue
	
	Block a user