Update app.py
Browse files
app.py
CHANGED
@@ -5,70 +5,71 @@ import vega_datasets
|
|
5 |
|
6 |
# Enable Panel extensions
|
7 |
pn.extension()
|
|
|
|
|
8 |
template = pn.template.BootstrapTemplate(
|
9 |
-
title='SI649
|
10 |
)
|
11 |
|
|
|
|
|
12 |
# Define a function to create and return a plot
|
13 |
-
def create_plot(subgroup):
|
14 |
-
# def create_plot(subgroup, date_range, moving_av_window):
|
15 |
|
16 |
# Apply any required transformations to the data in pandas)
|
17 |
filtered_df = df2_approve[df2_approve['subgroup'] == subgroup]
|
18 |
-
|
19 |
-
|
20 |
|
21 |
# Line chart
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
# Scatter plot with individual polls
|
28 |
-
scatter_plot = alt.Chart(filtered_df).mark_point(color='
|
29 |
x='timestamp:T',
|
30 |
y='rate:Q'
|
31 |
)
|
32 |
|
33 |
# Put them togetehr
|
34 |
-
plot = scatter_plot
|
35 |
|
36 |
# Return the combined chart
|
37 |
return plot
|
38 |
-
|
39 |
-
# date_range = ('2021-04-01', '2023-01-01')
|
40 |
-
|
41 |
-
# create_plot('All polls', date_range, 3)
|
42 |
|
43 |
|
44 |
# # Create the selection widget
|
45 |
-
# subgroup_widget = pn.widgets.Select(options=['All polls', 'Adults', 'Voters'], name='Subgroup')
|
46 |
select = pn.widgets.Select(name='Select', options=['All polls', 'Adults', 'Voters'])
|
47 |
|
48 |
|
49 |
# # Create the slider for the date range
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
# )
|
57 |
|
58 |
|
59 |
# # Create the slider for the moving average window
|
60 |
-
|
61 |
|
62 |
-
# create_plot(subgroup_widget, date_range_slider, moving_av_slider)
|
63 |
|
64 |
# Bind the widgets to the create_plot function
|
65 |
-
final = pn.bind(create_plot,
|
66 |
-
|
67 |
-
|
|
|
68 |
|
69 |
|
70 |
# # Combine everything in a Panel Column to create an app
|
71 |
-
|
|
|
|
|
|
|
|
|
72 |
|
73 |
# # set the app to be servable
|
74 |
-
|
|
|
5 |
|
6 |
# Enable Panel extensions
|
7 |
pn.extension()
|
8 |
+
pn.extension('vega', 'tabulator')
|
9 |
+
|
10 |
template = pn.template.BootstrapTemplate(
|
11 |
+
title='SI649 Altair3',
|
12 |
)
|
13 |
|
14 |
+
maincol = pn.Column()
|
15 |
+
|
16 |
# Define a function to create and return a plot
|
17 |
+
def create_plot(subgroup, date_range, moving_av_window):
|
|
|
18 |
|
19 |
# Apply any required transformations to the data in pandas)
|
20 |
filtered_df = df2_approve[df2_approve['subgroup'] == subgroup]
|
21 |
+
filtered_df = filtered_df[(filtered_df['timestamp'].dt.date >= date_range[0]) & (filtered_df['timestamp'].dt.date <= date_range[1])]
|
22 |
+
filtered_df['mov_avg'] = filtered_df['rate'].rolling(window=moving_av_window, min_periods=1).mean().shift(moving_av_window//2)
|
23 |
|
24 |
# Line chart
|
25 |
+
line_chart = alt.Chart(filtered_df).mark_line(color='red', size=2).encode(
|
26 |
+
x='timestamp:T',
|
27 |
+
y='mov_avg:Q'
|
28 |
+
)
|
29 |
|
30 |
# Scatter plot with individual polls
|
31 |
+
scatter_plot = alt.Chart(filtered_df).mark_point(color='grey', size=2, opacity=0.7).encode(
|
32 |
x='timestamp:T',
|
33 |
y='rate:Q'
|
34 |
)
|
35 |
|
36 |
# Put them togetehr
|
37 |
+
plot = scatter_plot + line_chart
|
38 |
|
39 |
# Return the combined chart
|
40 |
return plot
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
# # Create the selection widget
|
|
|
44 |
select = pn.widgets.Select(name='Select', options=['All polls', 'Adults', 'Voters'])
|
45 |
|
46 |
|
47 |
# # Create the slider for the date range
|
48 |
+
date_range_slider = pn.widgets.DateRangeSlider(
|
49 |
+
name='Date Range Slider',
|
50 |
+
start=df2['timestamp'].dt.date.min(), end=df2['timestamp'].dt.date.max(),
|
51 |
+
value=(df2['timestamp'].dt.date.min(), df2['timestamp'].dt.date.max()),
|
52 |
+
step=1
|
53 |
+
)
|
|
|
54 |
|
55 |
|
56 |
# # Create the slider for the moving average window
|
57 |
+
moving_av_slider = pn.widgets.IntSlider(name='Moving Average Window', start=1, end=100, value=1)
|
58 |
|
|
|
59 |
|
60 |
# Bind the widgets to the create_plot function
|
61 |
+
final = pn.Row(pn.bind(create_plot,
|
62 |
+
subgroup=select,
|
63 |
+
date_range=date_range_slider,
|
64 |
+
moving_av_window=moving_av_slider))
|
65 |
|
66 |
|
67 |
# # Combine everything in a Panel Column to create an app
|
68 |
+
maincol.append(final)
|
69 |
+
maincol.append(select)
|
70 |
+
maincol.append(date_range_slider)
|
71 |
+
maincol.append(moving_av_slider)
|
72 |
+
template.main.append(maincol)
|
73 |
|
74 |
# # set the app to be servable
|
75 |
+
template.show()
|