11import datetime
2+
23import pandas as pd
34import plotly .graph_objects as go
45import requests
5-
6- from shiny import App , render , ui , reactive
7- from shinywidgets import render_widget , output_widget
6+ from shiny import App , reactive , render , ui
7+ from shinywidgets import output_widget , render_widget
88
99app_ui = ui .page_sidebar (
1010 ui .sidebar (
11- ui .input_numeric ("lat" ,
12- "Latitude" ,
13- value = 51.3167 ),
14- ui .input_numeric ("lon" ,
15- "Longitude" ,
16- value = 9.5 )
11+ ui .input_numeric ("lat" , "Latitude" , value = 51.3167 ),
12+ ui .input_numeric ("lon" , "Longitude" , value = 9.5 ),
1713 ),
1814 output_widget ("plot" ),
1915 ui .output_table ("table" ),
20- title = "Weather App"
16+ title = "Weather App" ,
2117)
2218
2319
2420def server (input , output , session ):
2521 @reactive .Calc
2622 def weather_dat ():
27- url = f"https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&hourly=temperature_2m,rain&past_days=3" % (input .lat (), input .lon ())
23+ url = (
24+ f"https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&hourly=temperature_2m,rain&past_days=3"
25+ % (input .lat (), input .lon ())
26+ )
2827 response = requests .get (url )
2928 data = response .json ()["hourly" ]
3029 df = pd .DataFrame (data )
@@ -34,36 +33,33 @@ def weather_dat():
3433 def plot ():
3534 df = weather_dat ()
3635 fig = go .Figure ()
37- fig .add_trace (go .Scatter (x = df ['time' ], y = df ['temperature_2m' ], mode = 'lines' , name = 'Temperature' ))
38- fig .add_trace (go .Bar (x = df ['time' ], y = df ['rain' ], name = 'Rain' , yaxis = 'y2' ))
39- fig .add_vline (x = datetime .datetime .today ())
36+ fig .add_trace (
37+ go .Scatter (
38+ x = df ["time" ], y = df ["temperature_2m" ], mode = "lines" , name = "Temperature"
39+ )
40+ )
41+ fig .add_trace (go .Bar (x = df ["time" ], y = df ["rain" ], name = "Rain" , yaxis = "y2" ))
42+ fig .add_vline (x = datetime .datetime .today ())
4043 fig .update_layout (
41- xaxis_title = 'Date' ,
42- yaxis_title = 'Temperature (°C)' ,
43- yaxis2 = dict (
44- title = 'Rain (mm)' ,
45- overlaying = 'y' ,
46- side = 'right'
47- ),
48- showlegend = False
44+ xaxis_title = "Date" ,
45+ yaxis_title = "Temperature (°C)" ,
46+ yaxis2 = dict (title = "Rain (mm)" , overlaying = "y" , side = "right" ),
47+ showlegend = False ,
4948 )
5049 return fig
5150
52- @render .table (index = True )
51+ @render .table (index = True )
5352 def table ():
5453 df = weather_dat ()
55- df ['time' ] = pd .to_datetime (df ['time' ])
56- df .set_index ('time' , inplace = True )
57- daily_summary = df .resample ('D' ).agg ({
58- 'temperature_2m' : ['min' , 'max' ],
59- 'rain' : 'sum'
60- })
54+ df ["time" ] = pd .to_datetime (df ["time" ])
55+ df .set_index ("time" , inplace = True )
56+ daily_summary = df .resample ("D" ).agg (
57+ {"temperature_2m" : ["min" , "max" ], "rain" : "sum" }
58+ )
6159 daily_summary = daily_summary .T
62- daily_summary .index = [' min. T (°C)' , ' max. T (°C)' , ' Rain (mm)' ]
63- daily_summary .columns = [col .strftime (' %d.%m' ) for col in daily_summary .columns ]
60+ daily_summary .index = [" min. T (°C)" , " max. T (°C)" , " Rain (mm)" ]
61+ daily_summary .columns = [col .strftime (" %d.%m" ) for col in daily_summary .columns ]
6462 return daily_summary
6563
66-
67-
6864
6965app = App (app_ui , server )
0 commit comments