chriskok3 commited on
Commit
ec63963
·
verified ·
1 Parent(s): ff62565

Create app3.py

Browse files
Files changed (1) hide show
  1. app3.py +88 -0
app3.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # supress warnings about future deprecations
2
+ import warnings
3
+ warnings.simplefilter(action='ignore', category=FutureWarning)
4
+
5
+ import pandas as pd
6
+ import altair as alt
7
+ import numpy as np
8
+ import pprint
9
+ import datetime as dt
10
+ from vega_datasets import data
11
+ import matplotlib.pyplot as plt
12
+
13
+ # Import panel and vega datasets
14
+ import panel as pn
15
+ import vega_datasets
16
+
17
+
18
+ df2=pd.read_csv("https://raw.githubusercontent.com/dallascard/SI649_public/main/altair_hw3/approval_topline.csv")
19
+
20
+ # fix the time stamps and reorganize the data to combine approve and disapprove into one column
21
+ df2['timestamp']=pd.to_datetime(df2['timestamp'])
22
+ df2=pd.melt(df2, id_vars=['president', 'subgroup', 'timestamp'], value_vars=['approve','disapprove']).rename(columns={'variable':'choice', 'value':'rate'})
23
+
24
+ # Enable Panel extensions
25
+ pn.extension()
26
+
27
+ # Define a function to create and return a plot
28
+ def create_plot(subgroup='All polls', date_range=[df2['timestamp'].min(), df2['timestamp'].max()], moving_av_window=1):
29
+
30
+ # Downselection!
31
+ approval_df2 = df2.copy()
32
+ approval_df2 = approval_df2[approval_df2['subgroup'] == subgroup]
33
+ approval_df2 = approval_df2[approval_df2['choice'] == 'approve']
34
+ # remove columns
35
+ approval_df2 = approval_df2.drop(columns=['choice', 'subgroup', 'president'])
36
+
37
+ # convert date_range to datetime
38
+ date_range = [pd.to_datetime(date_range[0]), pd.to_datetime(date_range[1])]
39
+
40
+ # narrow down dates
41
+ approval_df2 = approval_df2[
42
+ (approval_df2['timestamp'] >= date_range[0]) &
43
+ (approval_df2['timestamp'] <= date_range[1])
44
+ ]
45
+
46
+ # Apply any required transformations to the data in pandas
47
+ smooth_data = approval_df2.sort_values('timestamp').rolling(window=moving_av_window, on='timestamp').mean().shift(moving_av_window*2)
48
+
49
+ chart_data = approval_df2.copy()
50
+
51
+ # Line chart
52
+ line = alt.Chart(smooth_data).mark_line(color='red', size=2).encode(
53
+ x=alt.X('timestamp:T', scale=alt.Scale(domain=date_range)),
54
+ y=alt.Y('rate:Q', scale=alt.Scale(domain=[30, 60]), title='approve, mov_avg'),
55
+ )
56
+
57
+ # Scatter plot with individual polls
58
+ scatter = alt.Chart(chart_data).mark_point(size=2, opacity=0.7, color='grey').encode(
59
+ x=alt.X('timestamp:T', scale=alt.Scale(domain=date_range)),
60
+ y=alt.Y('rate:Q', scale=alt.Scale(domain=[30, 60]))
61
+ )
62
+
63
+ # Put them together
64
+ plot = alt.layer(scatter, line).resolve_scale(y='shared')
65
+
66
+ # Return the combined chart
67
+ return plot
68
+
69
+ # Create the selection widget
70
+ subgroup_select = pn.widgets.Select(name='Select subgroup', options=['Adults', 'All polls', 'Voters'])
71
+
72
+ # Create the slider for the date range
73
+ date_range_slider = pn.widgets.DateRangeSlider(name='Date range slider', end=df2['timestamp'].max(), start=df2['timestamp'].min(), value=(df2['timestamp'].min(), df2['timestamp'].max()))
74
+
75
+ # Create the slider for the moving average window
76
+ moving_av_slider = pn.widgets.IntSlider(name='Moving average window', start=1, end=100, value=1)
77
+
78
+ # Bind the widgets to the create_plot function
79
+ @pn.depends(subgroup_select.param.value, date_range_slider.param.value, moving_av_slider.param.value)
80
+ def reactive_plot(subgroup, date_range, moving_av_window):
81
+ return create_plot(subgroup, date_range, moving_av_window)
82
+
83
+ # Combine everything in a Panel Column to create an app
84
+ app = pn.Column(reactive_plot, subgroup_select, date_range_slider, moving_av_slider)
85
+
86
+ # Set the app to be servable
87
+ app.servable()
88
+