jnaiman commited on
Commit
51d9500
1 Parent(s): 0231dd4
Files changed (4) hide show
  1. README.md +6 -6
  2. app.py +161 -0
  3. requirements.txt +8 -3
  4. test_altair_streamlit.py +0 -72
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Is445 Demo
3
- emoji: 🦀
4
- colorFrom: purple
5
- colorTo: indigo
6
  sdk: streamlit
7
- sdk_version: 1.37.0
8
  app_file: app.py
9
  pinned: false
10
- license: apache-2.0
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Streamlit Cookbook Code Examples
3
+ emoji: 🏢
4
+ colorFrom: blue
5
+ colorTo: gray
6
  sdk: streamlit
7
+ sdk_version: 1.36.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import altair as alt
5
+ import plotly.express as px
6
+ import plotly.graph_objects as go
7
+ import pydeck as pdk
8
+ from PIL import Image
9
+ import cv2
10
+ import io
11
+
12
+ st.set_page_config(page_title="Streamlit Comprehensive Demo 🚀", page_icon="📝", layout="wide")
13
+
14
+ def main():
15
+ st.title("🎨 Streamlit Comprehensive Showcase")
16
+ st.markdown("Explore the various features of Streamlit in this interactive demo!")
17
+
18
+ # Sidebar for navigation
19
+ demo_type = st.sidebar.selectbox(
20
+ "Choose a Demo",
21
+ ["Basic Elements", "Data Visualization", "Interactive Widgets", "Advanced Features"]
22
+ )
23
+
24
+ if demo_type == "Basic Elements":
25
+ show_basic_elements()
26
+ elif demo_type == "Data Visualization":
27
+ show_data_visualization()
28
+ elif demo_type == "Interactive Widgets":
29
+ show_interactive_widgets()
30
+ elif demo_type == "Advanced Features":
31
+ show_advanced_features()
32
+
33
+ def show_basic_elements():
34
+ st.header("1. Basic Elements")
35
+
36
+ st.subheader("1.1 Text Elements")
37
+ st.text("This is simple text")
38
+ st.markdown("This is **markdown** with _styling_")
39
+ st.latex(r"\begin{pmatrix}a & b \\ c & d\end{pmatrix}")
40
+
41
+ st.subheader("1.2 Data Display")
42
+ df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
43
+ st.dataframe(df)
44
+ st.table(df)
45
+ st.json({"foo": "bar", "baz": "boz"})
46
+
47
+ st.subheader("1.3 Media Elements")
48
+ st.image("https://streamlit.io/images/brand/streamlit-mark-color.png", width=200)
49
+ st.audio("https://upload.wikimedia.org/wikipedia/commons/c/c4/Muriel-Nguyen-Xuan-Chopin-valse-opus64-1.ogg")
50
+ st.video("https://youtu.be/B2iAodr0fOo")
51
+
52
+ def show_data_visualization():
53
+ st.header("2. Data Visualization")
54
+
55
+ @st.cache_data
56
+ def load_data():
57
+ return pd.DataFrame(np.random.randn(20, 3), columns=["A", "B", "C"])
58
+
59
+ data = load_data()
60
+
61
+ st.subheader("2.1 Streamlit Charts")
62
+ st.line_chart(data)
63
+ st.area_chart(data)
64
+ st.bar_chart(data)
65
+
66
+ st.subheader("2.2 Altair Chart")
67
+ chart = alt.Chart(data).mark_circle().encode(
68
+ x='A', y='B', size='C', color='C', tooltip=['A', 'B', 'C'])
69
+ st.altair_chart(chart, use_container_width=True)
70
+
71
+ st.subheader("2.3 Plotly Chart")
72
+ fig = px.scatter(data, x="A", y="B", size="C", color="C")
73
+ st.plotly_chart(fig, use_container_width=True)
74
+
75
+ st.subheader("2.4 PyDeck Chart")
76
+ chart_data = pd.DataFrame(np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=['lat', 'lon'])
77
+ st.pydeck_chart(pdk.Deck(
78
+ map_style=None,
79
+ initial_view_state=pdk.ViewState(latitude=37.76, longitude=-122.4, zoom=11, pitch=50),
80
+ layers=[pdk.Layer('HexagonLayer', data=chart_data, get_position='[lon, lat]', radius=200, elevation_scale=4, elevation_range=[0, 1000], pickable=True, extruded=True)]
81
+ ))
82
+
83
+ def show_interactive_widgets():
84
+ st.header("3. Interactive Widgets")
85
+
86
+ st.subheader("3.1 Input Widgets")
87
+ text_input = st.text_input("Enter some text")
88
+ number = st.number_input("Enter a number", min_value=0, max_value=100)
89
+ date = st.date_input("Pick a date")
90
+
91
+ st.subheader("3.2 Selection Widgets")
92
+ option = st.selectbox("Choose an option", ["Option 1", "Option 2", "Option 3"])
93
+ options = st.multiselect("Choose multiple options", ["A", "B", "C", "D"])
94
+ slider_val = st.slider("Pick a number", 0, 100)
95
+
96
+ st.subheader("3.3 Button and Download")
97
+ if st.button("Click me!"):
98
+ st.write("Button clicked!")
99
+
100
+ @st.cache_data
101
+ def get_sample_data():
102
+ return pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
103
+
104
+ csv = get_sample_data().to_csv(index=False)
105
+ st.download_button("Download CSV", csv, "sample_data.csv", "text/csv")
106
+
107
+ def show_advanced_features():
108
+ st.header("4. Advanced Features")
109
+
110
+ st.subheader("4.1 Layouts")
111
+ col1, col2 = st.columns(2)
112
+ with col1:
113
+ st.write("This is column 1")
114
+ with col2:
115
+ st.write("This is column 2")
116
+
117
+ with st.expander("Click to expand"):
118
+ st.write("This content is hidden by default")
119
+
120
+ st.subheader("4.2 Progress and Status")
121
+ progress_bar = st.progress(0)
122
+ for i in range(100):
123
+ progress_bar.progress(i + 1)
124
+ st.success("This is a success message!")
125
+ st.error("This is an error message!")
126
+ st.warning("This is a warning message!")
127
+ st.info("This is an info message!")
128
+
129
+ st.subheader("4.3 Cache and Performance")
130
+ @st.cache_data
131
+ def expensive_computation(a, b):
132
+ return a * b
133
+
134
+ result = expensive_computation(2, 21)
135
+ st.write(f"2 * 21 is {result}")
136
+
137
+ st.subheader("4.4 Session State")
138
+ if 'count' not in st.session_state:
139
+ st.session_state.count = 0
140
+
141
+ if st.button('Increment'):
142
+ st.session_state.count += 1
143
+
144
+ st.write('Count = ', st.session_state.count)
145
+
146
+ st.subheader("4.5 Forms")
147
+ with st.form("my_form"):
148
+ st.write("Inside the form")
149
+ slider_val = st.slider("Form slider")
150
+ checkbox_val = st.checkbox("Form checkbox")
151
+ submitted = st.form_submit_button("Submit")
152
+ if submitted:
153
+ st.write("Slider", slider_val, "Checkbox", checkbox_val)
154
+
155
+ st.subheader("4.6 Popover")
156
+ with st.popover("Click for Popover"):
157
+ st.write("This is a popover")
158
+ st.image("https://streamlit.io/images/brand/streamlit-mark-color.png", width=100)
159
+
160
+ if __name__ == "__main__":
161
+ main()
requirements.txt CHANGED
@@ -1,3 +1,8 @@
1
- streamlit
2
- altair
3
- vega_datasets
 
 
 
 
 
 
1
+ streamlit==1.36.0
2
+ pandas==1.5.3
3
+ numpy==1.23.5
4
+ altair==5.0.1
5
+ plotly==5.14.1
6
+ pydeck==0.8.0
7
+ Pillow==9.5.0
8
+ opencv-python-headless==4.7.0.72
test_altair_streamlit.py DELETED
@@ -1,72 +0,0 @@
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 test_altair_streamlit.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
- # stuff
8
-
9
-
10
- import streamlit as st
11
- import altair as alt
12
- from vega_datasets import data
13
-
14
- st.title('My First Streamlit App')
15
-
16
- source = data.seattle_weather()
17
-
18
- scale = alt.Scale(
19
- domain=["sun", "fog", "drizzle", "rain", "snow"],
20
- range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"],
21
- )
22
- color = alt.Color("weather:N", scale=scale)
23
-
24
- # We create two selections:
25
- # - a brush that is active on the top panel
26
- # - a multi-click that is active on the bottom panel
27
- brush = alt.selection_interval(encodings=["x"])
28
- click = alt.selection_point(encodings=["color"])
29
-
30
- # Top panel is scatter plot of temperature vs time
31
- points = (
32
- alt.Chart()
33
- .mark_point()
34
- .encode(
35
- alt.X("monthdate(date):T", title="Date"),
36
- alt.Y(
37
- "temp_max:Q",
38
- title="Maximum Daily Temperature (C)",
39
- scale=alt.Scale(domain=[-5, 40]),
40
- ),
41
- color=alt.condition(brush, color, alt.value("lightgray")),
42
- size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])),
43
- )
44
- .properties(width=550, height=300)
45
- .add_params(brush)
46
- .transform_filter(click)
47
- )
48
-
49
- # Bottom panel is a bar chart of weather type
50
- bars = (
51
- alt.Chart()
52
- .mark_bar()
53
- .encode(
54
- x="count()",
55
- y="weather:N",
56
- color=alt.condition(click, color, alt.value("lightgray")),
57
- )
58
- .transform_filter(brush)
59
- .properties(
60
- width=550,
61
- )
62
- .add_params(click)
63
- )
64
-
65
- chart = alt.vconcat(points, bars, data=source, title="Seattle Weather: 2012-2015")
66
-
67
- tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
68
-
69
- with tab1:
70
- st.altair_chart(chart, theme="streamlit", use_container_width=True)
71
- with tab2:
72
- st.altair_chart(chart, theme=None, use_container_width=True)