Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,173 @@
|
|
| 1 |
-
# load up the libraries
|
| 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 |
-
# we want to use vega, tell Panel to load up what we need
|
| 11 |
-
pn.extension('vega')
|
| 12 |
-
|
| 13 |
-
# create a basic template using bootstrap
|
| 14 |
-
template = pn.template.BootstrapTemplate(
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
# the main column will hold our key content
|
| 19 |
-
maincol = pn.Column()
|
| 20 |
-
|
| 21 |
-
# add some markdown to the main column
|
| 22 |
-
maincol.append("# Markdown Title")
|
| 23 |
-
maincol.append("I can format in cool ways. Like **bold** or *italics* or ***both*** or ~~strikethrough~~ or `code` or [links](https://panel.holoviz.org)")
|
| 24 |
-
maincol.append("I am writing a link [to the streamlit documentation page](https://docs.streamlit.io/en/stable/api.html)")
|
| 25 |
-
maincol.append('')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# load up a dataframe and show it in the main column
|
| 28 |
-
cars_url = "https://raw.githubusercontent.com/altair-viz/vega_datasets/master/vega_datasets/_data/cars.json"
|
| 29 |
-
cars = pd.read_json(cars_url)
|
| 30 |
-
temps = data.seattle_weather()
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
y='Miles_per_Gallon:Q',
|
| 38 |
-
color='Origin:N'
|
| 39 |
-
)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 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 |
-
#
|
| 63 |
-
|
| 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 |
-
# create a selection widget
|
| 91 |
-
select = pn.widgets.Select(name='Select', options=['Horsepower','Acceleration','Miles_per_Gallon'])
|
| 92 |
-
|
| 93 |
-
# create a function to modify the basechart that is being
|
| 94 |
-
# held in currentoption
|
| 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 |
+
# # load up the libraries
|
| 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 |
+
# # we want to use vega, tell Panel to load up what we need
|
| 11 |
+
# pn.extension('vega')
|
| 12 |
+
|
| 13 |
+
# # create a basic template using bootstrap
|
| 14 |
+
# template = pn.template.BootstrapTemplate(
|
| 15 |
+
# title='SI649 Walkthrough',
|
| 16 |
+
# )
|
| 17 |
+
|
| 18 |
+
# # the main column will hold our key content
|
| 19 |
+
# maincol = pn.Column()
|
| 20 |
+
|
| 21 |
+
# # add some markdown to the main column
|
| 22 |
+
# maincol.append("# Markdown Title")
|
| 23 |
+
# maincol.append("I can format in cool ways. Like **bold** or *italics* or ***both*** or ~~strikethrough~~ or `code` or [links](https://panel.holoviz.org)")
|
| 24 |
+
# maincol.append("I am writing a link [to the streamlit documentation page](https://docs.streamlit.io/en/stable/api.html)")
|
| 25 |
+
# maincol.append('')
|
| 26 |
+
|
| 27 |
+
# # load up a dataframe and show it in the main column
|
| 28 |
+
# cars_url = "https://raw.githubusercontent.com/altair-viz/vega_datasets/master/vega_datasets/_data/cars.json"
|
| 29 |
+
# cars = pd.read_json(cars_url)
|
| 30 |
+
# temps = data.seattle_weather()
|
| 31 |
+
|
| 32 |
+
# maincol.append(temps.head(10))
|
| 33 |
+
|
| 34 |
+
# # create a basic chart
|
| 35 |
+
# hp_mpg = alt.Chart(cars).mark_circle(size=80).encode(
|
| 36 |
+
# x='Horsepower:Q',
|
| 37 |
+
# y='Miles_per_Gallon:Q',
|
| 38 |
+
# color='Origin:N'
|
| 39 |
+
# )
|
| 40 |
+
|
| 41 |
+
# # dispaly it in the main column
|
| 42 |
+
# # maincol.append(hp_mpg)
|
| 43 |
+
|
| 44 |
+
# # create a basic slider
|
| 45 |
+
# simpleslider = pn.widgets.IntSlider(name='Simple Slider', start=0, end=100, value=0)
|
| 46 |
+
|
| 47 |
+
# # generate text based on slider value
|
| 48 |
+
# def square(x):
|
| 49 |
+
# return f'{x} squared is {x**2}'
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# # bind the slider to the function and hold the output in a row
|
| 53 |
+
# row = pn.Column(pn.bind(square,simpleslider))
|
| 54 |
+
|
| 55 |
+
# # add both slider and row
|
| 56 |
+
# maincol.append(simpleslider)
|
| 57 |
+
# maincol.append(row)
|
| 58 |
+
|
| 59 |
+
# # variable to track state of visualization
|
| 60 |
+
# flip = False
|
| 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 |
+
# # create a selection widget
|
| 91 |
+
# select = pn.widgets.Select(name='Select', options=['Horsepower','Acceleration','Miles_per_Gallon'])
|
| 92 |
+
|
| 93 |
+
# # create a function to modify the basechart that is being
|
| 94 |
+
# # held in currentoption
|
| 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")
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
import panel as pn
|
| 121 |
+
import pandas as pd
|
| 122 |
+
import altair as alt
|
| 123 |
|
| 124 |
+
#load data
|
| 125 |
+
df1=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_polllist.csv")
|
| 126 |
+
df2=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_topline.csv")
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
# fix the time stamps and reorganize the data to combine approve and disapprove into one column
|
| 129 |
+
df2['timestamp']=pd.to_datetime(df2['timestamp'])
|
| 130 |
+
df2=pd.melt(df2, id_vars=['president', 'subgroup', 'timestamp'], value_vars=['approve','disapprove']).rename(columns={'variable':'choice', 'value':'rate'})
|
| 131 |
|
| 132 |
+
pn.extension('vega')
|
|
|
|
| 133 |
|
| 134 |
+
# Selection widgets
|
| 135 |
+
subgroup_select = pn.widgets.Select(name='Select', options=['Adults', 'Voters', 'All polls'])
|
| 136 |
+
date_slider = pn.widgets.DateRangeSlider(name='Date Range Slider', start=df2['timestamp'].min(), end=df2['timestamp'].max())
|
| 137 |
+
moving_avg_slider = pn.widgets.IntSlider(name='Moving Average Window', start=1, end=100, step=1)
|
| 138 |
+
|
| 139 |
+
# Bind the widgets to the create_plot function
|
| 140 |
+
@pn.depends(subgroup_select.param.value, date_slider.param.value, moving_avg_slider.param.value)
|
| 141 |
+
def create_plot(subgroup, date_range, moving_av_window):
|
| 142 |
+
data = df2[(df2['subgroup'] == subgroup) & (df2['choice'] == "approve") & (df2['timestamp'].dt.date.between(date_range[0], date_range[1]))]
|
| 143 |
+
min_rate = df2['rate'].min()
|
| 144 |
+
max_rate = df2['rate'].max()
|
| 145 |
+
|
| 146 |
+
data = data.sort_values('timestamp')
|
| 147 |
+
data['moving_avg'] = data['rate'].rolling(window=moving_av_window, min_periods=1).mean()
|
| 148 |
+
|
| 149 |
+
# Line chart for moving average
|
| 150 |
+
line = alt.Chart(data).mark_line(interpolate='natural').encode(
|
| 151 |
+
x=alt.X('timestamp:T', axis=alt.Axis(title='', format='%b %d, %Y')),
|
| 152 |
+
y=alt.Y('moving_avg:Q', axis=alt.Axis(title='approve,mov_avg'), scale=alt.Scale(domain=[min_rate, max_rate])),
|
| 153 |
+
color=alt.value('red')
|
| 154 |
+
)
|
| 155 |
|
| 156 |
+
# Scatter plot for individual data points
|
| 157 |
+
points = alt.Chart(data).mark_point().encode(
|
| 158 |
+
x=alt.X('timestamp:T'),
|
| 159 |
+
y=alt.Y('rate:Q', axis=alt.Axis(title='approve,mov_avg'), scale=alt.Scale(domain=[min_rate, max_rate])),
|
| 160 |
+
color=alt.value('gray'),
|
| 161 |
+
tooltip=['timestamp:T', 'rate:Q']
|
| 162 |
+
)
|
| 163 |
|
| 164 |
+
# Combine line chart and scatter plot
|
| 165 |
+
plot = alt.layer(points, line).resolve_scale(y='shared')
|
| 166 |
|
| 167 |
+
return plot
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
# Combine everything in a Panel Column to create an app
|
| 170 |
+
app = pn.Column("# Polling Data Interactive Visualization", pn.panel(create_plot, reactive=True), subgroup_select, date_slider, moving_avg_slider)
|
| 171 |
|
| 172 |
+
# Set the app to be servable
|
| 173 |
+
app.servable()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|