jnaiman commited on
Commit
2e0b90f
·
1 Parent(s): e2bd642
Files changed (2) hide show
  1. requirements.txt +3 -0
  2. test_altair_streamlit.py +71 -0
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ altair
3
+ vega_datasets
test_altair_streamlit.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+
9
+ import streamlit as st
10
+ import altair as alt
11
+ from vega_datasets import data
12
+
13
+ st.title('My First Streamlit App')
14
+
15
+ source = data.seattle_weather()
16
+
17
+ scale = alt.Scale(
18
+ domain=["sun", "fog", "drizzle", "rain", "snow"],
19
+ range=["#e7ba52", "#a7a7a7", "#aec7e8", "#1f77b4", "#9467bd"],
20
+ )
21
+ color = alt.Color("weather:N", scale=scale)
22
+
23
+ # We create two selections:
24
+ # - a brush that is active on the top panel
25
+ # - a multi-click that is active on the bottom panel
26
+ brush = alt.selection_interval(encodings=["x"])
27
+ click = alt.selection_point(encodings=["color"])
28
+
29
+ # Top panel is scatter plot of temperature vs time
30
+ points = (
31
+ alt.Chart()
32
+ .mark_point()
33
+ .encode(
34
+ alt.X("monthdate(date):T", title="Date"),
35
+ alt.Y(
36
+ "temp_max:Q",
37
+ title="Maximum Daily Temperature (C)",
38
+ scale=alt.Scale(domain=[-5, 40]),
39
+ ),
40
+ color=alt.condition(brush, color, alt.value("lightgray")),
41
+ size=alt.Size("precipitation:Q", scale=alt.Scale(range=[5, 200])),
42
+ )
43
+ .properties(width=550, height=300)
44
+ .add_params(brush)
45
+ .transform_filter(click)
46
+ )
47
+
48
+ # Bottom panel is a bar chart of weather type
49
+ bars = (
50
+ alt.Chart()
51
+ .mark_bar()
52
+ .encode(
53
+ x="count()",
54
+ y="weather:N",
55
+ color=alt.condition(click, color, alt.value("lightgray")),
56
+ )
57
+ .transform_filter(brush)
58
+ .properties(
59
+ width=550,
60
+ )
61
+ .add_params(click)
62
+ )
63
+
64
+ chart = alt.vconcat(points, bars, data=source, title="Seattle Weather: 2012-2015")
65
+
66
+ tab1, tab2 = st.tabs(["Streamlit theme (default)", "Altair native theme"])
67
+
68
+ with tab1:
69
+ st.altair_chart(chart, theme="streamlit", use_container_width=True)
70
+ with tab2:
71
+ st.altair_chart(chart, theme=None, use_container_width=True)