Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,74 @@
|
|
1 |
-
#
|
2 |
-
import panel as pn
|
3 |
-
import pandas as pd
|
4 |
-
import altair as alt
|
5 |
-
from vega_datasets import data
|
6 |
-
|
7 |
-
# we want to use bootstrap/template, tell Panel to load up what we need
|
8 |
-
pn.extension(design='bootstrap')
|
9 |
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
#
|
|
|
14 |
template = pn.template.BootstrapTemplate(
|
15 |
-
title='SI649
|
16 |
)
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
-
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
x='Horsepower:Q',
|
37 |
-
y='Miles_per_Gallon:Q',
|
38 |
-
color='Origin:N'
|
39 |
-
)
|
40 |
|
41 |
-
#
|
42 |
-
#
|
|
|
43 |
|
44 |
-
# create a basic slider
|
45 |
-
simpleslider = pn.widgets.IntSlider(name='Simple Slider', start=0, end=100, value=0)
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
|
55 |
-
#
|
56 |
-
maincol.append(simpleslider)
|
57 |
-
maincol.append(row)
|
58 |
|
59 |
-
#
|
60 |
-
|
|
|
61 |
|
62 |
-
# function to either return the vis or a message
|
63 |
-
def makeChartVisible(val):
|
64 |
-
global flip # grab the variable outside the function
|
65 |
-
if (flip == True):
|
66 |
-
flip = not flip # flip to False
|
67 |
-
return pn.pane.Vega(hp_mpg) # return the vis
|
68 |
-
else:
|
69 |
-
flip = not flip # flip to true and return text
|
70 |
-
return pn.panel("Click the button to see the chart")
|
71 |
-
|
72 |
-
# add a button and then create the binding
|
73 |
-
btn = pn.widgets.Button(name='Click me')
|
74 |
-
row = pn.Row(pn.bind(makeChartVisible, btn))
|
75 |
-
|
76 |
-
# add button and new row to main column
|
77 |
-
maincol.append(btn)
|
78 |
-
maincol.append(row)
|
79 |
-
|
80 |
-
# create a base chart
|
81 |
-
basechart = alt.Chart(cars).mark_circle(size=80,opacity=0.5).encode(
|
82 |
-
x='Horsepower:Q',
|
83 |
-
y='Acceleration:Q',
|
84 |
-
color="Origin:N"
|
85 |
-
)
|
86 |
|
87 |
-
# create something to hold the base chart
|
88 |
-
currentoption = pn.panel(basechart)
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
|
93 |
-
#
|
94 |
-
#
|
95 |
-
def changeOption(val):
|
96 |
-
# grab what's there now
|
97 |
-
chrt = currentoption.object
|
98 |
-
# change the encoding based on val
|
99 |
-
chrt = chrt.encode(
|
100 |
-
y=val+":Q"
|
101 |
-
)
|
102 |
-
# replace old chart in currentoption with new one
|
103 |
-
currentoption.object = chrt
|
104 |
-
|
105 |
-
# append the selection
|
106 |
-
maincol.append(select)
|
107 |
-
# append the binding (in thise case nothing is being returned by changeOption, so...)
|
108 |
-
chartchange = pn.Row(pn.bind(changeOption, select))
|
109 |
-
# ... we need to also add the chart
|
110 |
-
maincol.append(chartchange)
|
111 |
-
maincol.append(currentoption)
|
112 |
-
|
113 |
-
# add the main column to the template
|
114 |
-
template.main.append(maincol)
|
115 |
-
|
116 |
-
# Indicate that the template object is the "application" and serve it
|
117 |
-
template.servable(title="SI649 Walkthrough")
|
|
|
1 |
+
# Import panel and vega datasets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import panel as pn
|
4 |
+
import vega_datasets
|
5 |
|
6 |
+
# Enable Panel extensions
|
7 |
+
pn.extension()
|
8 |
template = pn.template.BootstrapTemplate(
|
9 |
+
title='SI649 Lab7',
|
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 |
+
# filtered_df = filtered_df[(filtered_df['timestamp'] >= date_range[0]) & (filtered_df['timestamp'] <= date_range[1])]
|
19 |
+
# filtered_df['Smoothed_Rate'] = filtered_df['rate'].rolling(window=moving_av_window, min_periods=1).mean().shift()
|
20 |
+
|
21 |
+
# Line chart
|
22 |
+
# line_chart = alt.Chart(filtered_df).mark_line(color='red').encode(
|
23 |
+
# x='timestamp:T',
|
24 |
+
# y='Smoothed_Rate:Q'
|
25 |
+
# )
|
26 |
+
|
27 |
+
# Scatter plot with individual polls
|
28 |
+
scatter_plot = alt.Chart(filtered_df).mark_point(color='gray', size=2, opacity=0.7).encode(
|
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 |
+
# date_range_slider = pn.widgets.DateRangeSlider(name='Date Range')
|
51 |
+
# date_range_slider = pn.widgets.DateRangeSlider(
|
52 |
+
# name='Date Range Slider',
|
53 |
+
# start=dt.datetime(2017, 1, 1), end=dt.datetime(2019, 1, 1),
|
54 |
+
# value=(dt.datetime(2017, 1, 1), dt.datetime(2018, 1, 10)),
|
55 |
+
# step=2
|
56 |
+
# )
|
57 |
|
58 |
|
59 |
+
# # Create the slider for the moving average window
|
60 |
+
# moving_av_slider = pn.widgets.IntSlider(name='Moving Average Window', start=1, end=10, value=3)
|
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,select)
|
66 |
+
template.main.append(final)
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
|
|
|
|
69 |
|
70 |
+
# # Combine everything in a Panel Column to create an app
|
71 |
+
# app = pn.Column(subgroup_widget, date_range_slider, moving_av_slider, update_plot)
|
72 |
|
73 |
+
# # set the app to be servable
|
74 |
+
# app.servable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|