Skip to content

Commit c2ea0b3

Browse files
committed
Merge branch 'hbl_add_basic' into 'main'
add basic apps See merge request insertnamehere/shiny/shiny-for-python-getting-started!3
2 parents bad6e0d + 3b55bd3 commit c2ea0b3

File tree

9 files changed

+321
-71
lines changed

9 files changed

+321
-71
lines changed

basic_app/README.md

Whitespace-only changes.

basic_app/app.py

+54-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
1-
from shiny import render, ui
1+
import datetime
2+
import pandas as pd
3+
import plotly.graph_objects as go
4+
import requests
5+
6+
from shiny import render, ui, reactive
27
from shiny.express import input
8+
from shinywidgets import render_widget
9+
10+
11+
ui.panel_title("Weather App")
12+
ui.input_numeric("lat",
13+
"Latitude",
14+
value = 51.3167)
15+
ui.input_numeric("lon",
16+
"Longitude",
17+
value = 9.5)
318

4-
ui.panel_title("Hello Shiny!")
5-
ui.input_slider("n", "N", 0, 100, 20)
19+
@reactive.Calc
20+
def weather_dat():
21+
url = f"https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s&hourly=temperature_2m,rain&past_days=3" %(input.lat(), input.lon())
22+
response = requests.get(url)
23+
data = response.json()["hourly"]
24+
df = pd.DataFrame(data)
25+
return df
626

27+
@render_widget
28+
def plot():
29+
df = weather_dat()
30+
fig = go.Figure()
31+
fig.add_trace(go.Scatter(x=df['time'], y=df['temperature_2m'], mode='lines', name='Temperature'))
32+
fig.add_trace(go.Bar(x=df['time'], y=df['rain'], name='Rain', yaxis='y2'))
33+
fig.add_vline(x = datetime.datetime.today())
34+
fig.update_layout(
35+
xaxis_title='Date',
36+
yaxis_title='Temperature (°C)',
37+
yaxis2=dict(
38+
title='Rain (mm)',
39+
overlaying='y',
40+
side='right'
41+
),
42+
showlegend = False
43+
)
44+
return fig
745

8-
@render.text
9-
def txt():
10-
return f"n*2 is {input.n() * 2}"
46+
@render.table(index= True)
47+
def table():
48+
df = weather_dat()
49+
df['time'] = pd.to_datetime(df['time'])
50+
df.set_index('time', inplace=True)
51+
daily_summary = df.resample('D').agg({
52+
'temperature_2m': ['min', 'max'],
53+
'rain': 'sum'
54+
})
55+
daily_summary = daily_summary.T
56+
daily_summary.index = ['min. T (°C)', 'max. T (°C)', 'Rain (mm)']
57+
daily_summary.columns = [col.strftime('%d.%m') for col in daily_summary.columns]
58+
return daily_summary

basic_app/hello.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def main():
2+
print("Hello from basic-app!")
3+
4+
5+
if __name__ == "__main__":
6+
main()

basic_app/pyproject.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "basic-app"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
dependencies = [
8+
"jinja2>=3.1.5",
9+
"pandas>=2.2.3",
10+
"plotly>=6.0.0",
11+
"requests>=2.32.3",
12+
"shiny>=1.2.1",
13+
"shinywidgets>=0.5.1",
14+
]

basic_app_core/app.py

+61-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
1-
from shiny import App, render, ui
1+
import datetime
2+
import pandas as pd
3+
import plotly.graph_objects as go
4+
import requests
25

3-
app_ui = ui.page_fluid(
4-
ui.panel_title("Hello Shiny!"),
5-
ui.input_slider("n", "N", 0, 100, 20),
6-
ui.output_text_verbatim("txt"),
6+
from shiny import App, render, ui, reactive
7+
from shinywidgets import render_widget, output_widget
8+
9+
app_ui = ui.page_sidebar(
10+
ui.sidebar(
11+
ui.input_numeric("lat",
12+
"Latitude",
13+
value = 51.3167),
14+
ui.input_numeric("lon",
15+
"Longitude",
16+
value = 9.5)
17+
),
18+
output_widget("plot"),
19+
ui.output_table("table"),
20+
title = "Weather App"
721
)
822

923

1024
def server(input, output, session):
11-
@render.text
12-
def txt():
13-
return f"n*2 is {input.n() * 2}"
25+
@reactive.Calc
26+
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())
28+
response = requests.get(url)
29+
data = response.json()["hourly"]
30+
df = pd.DataFrame(data)
31+
return df
32+
33+
@render_widget
34+
def plot():
35+
df = weather_dat()
36+
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())
40+
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
49+
)
50+
return fig
51+
52+
@render.table(index= True)
53+
def table():
54+
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+
})
61+
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]
64+
return daily_summary
65+
66+
1467

1568

1669
app = App(app_ui, server)

basic_app_core/hello.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def main():
2+
print("Hello from basic-app-core!")
3+
4+
5+
if __name__ == "__main__":
6+
main()

basic_app_core/pyproject.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "basic-app-core"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
dependencies = [
8+
"jinja2>=3.1.5",
9+
"pandas>=2.2.3",
10+
"plotly>=6.0.0",
11+
"requests>=2.32.3",
12+
"shiny>=1.2.1",
13+
"shinywidgets>=0.5.1",
14+
]

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ dependencies = [
1111
]
1212

1313
[tool.uv.workspace]
14-
members = ["routes_example_app", "shinylive_quarto", "shinylive_without_quarto", "app_from_gh_template"]
14+
members = ["routes_example_app", "shinylive_quarto", "shinylive_without_quarto", "app_from_gh_template", "basic_app", "basic_app_core"]

0 commit comments

Comments
 (0)