jnaiman commited on
Commit
8428d66
·
1 Parent(s): 616e50d
Files changed (3) hide show
  1. README.md +11 -1
  2. app.py +88 -57
  3. requirements.txt +3 -1
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: My IS445 Example ID13111
3
  emoji: 🏢
4
  colorFrom: blue
5
  colorTo: gray
@@ -11,3 +11,13 @@ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Prep notebook -- My Streamlit App
3
  emoji: 🏢
4
  colorFrom: blue
5
  colorTo: gray
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ # Info:
16
+ This is the "prep" notebook for a introductory Streamlit app for IS445.
17
+
18
+ ## How to teach with this:
19
+ We will build this "from scratch" during class using the following steps:
20
+ 1. Make sure your repo is cloned in your PraireLearn (this will be "is445_demo2" with my setup, "is445_demo" for each of the students)
21
+ 1. Update the README.md if the app.py file has changed names
22
+ 1. Copy what we did from last week to the app.py file here
23
+ 1. Update the requirements.txt file to include libraries (see whats in here now)
app.py CHANGED
@@ -1,73 +1,104 @@
1
- # INSTRUCTIONS:
2
- # 1. Open a "Terminal" by: View --> Terminal OR just the "Terminal" through the hamburger menu
3
- # 2. run in terminal with: streamlit run app.py
4
- # 3. click the "Open in Browser" link that pops up OR click on "Ports" and copy the URL
5
- # 4. Open a Simple Browswer with View --> Command Palette --> Simple Browser: Show
6
- # 5. use the URL from prior steps as intput into this simple browser
7
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  import streamlit as st
10
  import altair as alt
11
- from vega_datasets import data
12
-
13
- st.title('Streamlit App for IS445: ID46435')
14
 
15
- st.text("The URL for this app is: https://huggingface.co/spaces/jnaiman/my_test_demo")
 
 
16
 
17
- source = data.seattle_weather()
18
 
19
- scale = alt.Scale(
20
- domain=["sun", "fog", "drizzle", "rain", "snow"],
21
- range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"],
 
 
22
  )
23
- color = alt.Color("weather:N", scale=scale)
24
-
25
- # We create two selections:
26
- # - a brush that is active on the top panel
27
- # - a multi-click that is active on the bottom panel
28
- brush = alt.selection_interval(encodings=["x"])
29
- click = alt.selection_point(encodings=["color"])
30
-
31
- # Top panel is scatter plot of temperature vs time
32
- points = (
33
- alt.Chart()
34
- .mark_point()
35
- .encode(
36
- alt.X("monthdate(date):T", title="Date (Month Year)"),
37
- alt.Y(
38
- "temp_max:Q",
39
- title="Maximum Daily Temperature (C)",
40
- scale=alt.Scale(domain=[-5, 40]),
41
- ),
42
- color=alt.condition(brush, color, alt.value("lightgray")),
43
- size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])),
44
- )
45
- .properties(width=550, height=300)
46
- .add_params(brush)
47
- .transform_filter(click)
48
  )
49
 
50
- # Bottom panel is a bar chart of weather type
51
- bars = (
52
- alt.Chart()
53
- .mark_bar()
54
- .encode(
55
- x="count()",
56
- y="weather:N",
57
- color=alt.condition(click, color, alt.value("lightgray")),
58
- )
59
- .transform_filter(brush)
60
- .properties(
61
- width=550,
62
- )
63
- .add_params(click)
64
  )
65
 
66
- chart = alt.vconcat(points, bars, data=source, title="Seattle Weather - 2007 to 2013")
67
 
68
- tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
69
 
70
  with tab1:
71
- st.altair_chart(chart, theme="streamlit", use_container_width=True)
72
  with tab2:
73
- st.altair_chart(chart, theme=None, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
 
3
+
4
+ # day 2/3 -- "grab bag" of other things
5
+ # touch on widgets! -- do quick example of plot + widget, say we'll focus on Altair
6
+ # multi-page apps? ==> maybe day 2? ==> does this work with HF apps??
7
+ # matplotlib plots
8
+ # other plotting tools in ST (like the defaults) + widgets
9
+ #* https://docs.streamlit.io/develop/tutorials/databases <- touch on but say we'll be just doing csv files
10
+ # embedding streamlit spaces on other webpages? wait until Jekyll? https://huggingface.co/docs/hub/en/spaces-sdks-streamlit#embed-streamlit-spaces-on-other-webpages
11
+ # how to search/duplicate other spaces on HF (make sure you cite this!)
12
+
13
+ # start with "this is how we publish with streamlit" -- README file, requirements, etc
14
+ # ---> make sure to mention the "yaml-ness" of the README file
15
+ # ---> say that the easiest way to start is with an already hosted app on HF -- luckily we alread have a lab on this!
16
+ # ---> make this like the "jekyll updates" folders that have all these prep and in class files in them
17
+ # Then: more streamlit extras with all of those ones listed above
18
+
19
+ ################################################
20
+ # 1. Review of where we got to last time
21
+ ################################################
22
+
23
+
24
+ # Let's start by copying things we did last time
25
  import streamlit as st
26
  import altair as alt
 
 
 
27
 
28
+ # Let's recall a plot that we made with Altair in Jupyterlab:
29
+ # Make sure we copy the URL as well!
30
+ mobility_url = 'https://raw.githubusercontent.com/UIUC-iSchool-DataViz/is445_data/main/mobility.csv'
31
 
32
+ st.title('This is my fancy app for HuggingFace!!')
33
 
34
+ scatters = alt.Chart(mobility_url).mark_point().encode(
35
+ x='Mobility:Q', # "Q for quantiative"
36
+ #y='Population:Q',
37
+ y=alt.Y('Population:Q', scale=alt.Scale(type='log')),
38
+ color=alt.Color('Income:Q', scale=alt.Scale(scheme='sinebow'),bin=alt.Bin(maxbins=5))
39
  )
40
+
41
+ st.header('More complex Dashboards')
42
+
43
+ brush = alt.selection_interval(encodings=['x','y'])
44
+
45
+ chart1 = alt.Chart(mobility_url).mark_rect().encode(
46
+ alt.X("Student_teacher_ratio:Q", bin=alt.Bin(maxbins=10)),
47
+ alt.Y("State:O"),
48
+ alt.Color("count()")
49
+ ).properties(
50
+ height=400
51
+ ).add_params(
52
+ brush
 
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
+ chart2 = alt.Chart(mobility_url).mark_bar().encode(
56
+ alt.X("Mobility:Q", bin=True,axis=alt.Axis(title='Mobility Score')),
57
+ alt.Y('count()', axis=alt.Axis(title='Mobility Score Distribution'))
58
+ ).transform_filter(
59
+ brush
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
+ chart = (chart1.properties(width=300) | chart2.properties(width=300))
63
 
64
+ tab1, tab2 = st.tabs(["Mobility interactive", "Scatter plot"])
65
 
66
  with tab1:
67
+ st.altair_chart(chart, theme=None, use_container_width=True)
68
  with tab2:
69
+ st.altair_chart(scatters, theme=None, use_container_width=True)
70
+
71
+
72
+ ################################################
73
+ # 2. Adding features, Pushing to HF
74
+ ################################################
75
+
76
+ st.header('Requirements, README file, Pushing to HuggingFace')
77
+
78
+ ### 2.1 Make a plot ###
79
+
80
+ # Let's say we want to add in some matplotlib plots from some data we read
81
+ # in with Pandas.
82
+
83
+ import pandas as pd
84
+ df = pd.read_csv(mobility_url)
85
+
86
+ # There are a few ways to show the dataframe if we want our viewer to see the table:
87
+ #df
88
+ st.write(df)
89
+
90
+ # Now, let's plot with matplotlib:
91
+ import matplotlib.pyplot as plt
92
+
93
+ fig, ax = plt.subplots()
94
+ df['Seg_income'].plot(kind='hist', ax=ax)
95
+ #plt.show() # but wait! this doesn't work!
96
+
97
+ # We need to use the streamlit-specific way of showing matplotlib plots: https://docs.streamlit.io/develop/api-reference/charts/st.pyplot
98
+ st.pyplot(fig)
99
+
100
+ ### 2.2 Push these changes to HF ###
101
+ # In order to push these changes to HF and have things actually show up we need to
102
+ # add the packages we've added to our requirements.txt file.
103
+
104
+ # While we're doing this, let's also take a look at the README.md file!
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  streamlit
2
  altair
3
- vega_datasets
 
 
 
1
  streamlit
2
  altair
3
+ numpy
4
+ pandas
5
+ matplotlib