├── .devcontainer └── devcontainer.json ├── Hello.py ├── README.md ├── __init__.py ├── pages ├── 0_Animation_Demo.py ├── 1_Plotting_Demo.py ├── 2_Mapping_Demo.py └── 3_DataFrame_Demo.py ├── requirements.txt └── utils.py /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Python 3", 3 | // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile 4 | "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye", 5 | "customizations": { 6 | "codespaces": { 7 | "openFiles": [ 8 | "README.md", 9 | "Hello.py" 10 | ] 11 | }, 12 | "vscode": { 13 | "settings": {}, 14 | "extensions": [ 15 | "ms-python.python", 16 | "ms-python.vscode-pylance" 17 | ] 18 | } 19 | }, 20 | "updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y None: 24 | 25 | # Interactive Streamlit elements, like these sliders, return their value. 26 | # This gives you an extremely simple interaction model. 27 | iterations = st.sidebar.slider("Level of detail", 2, 20, 10, 1) 28 | separation = st.sidebar.slider("Separation", 0.7, 2.0, 0.7885) 29 | 30 | # Non-interactive elements return a placeholder to their location 31 | # in the app. Here we're storing progress_bar to update it later. 32 | progress_bar = st.sidebar.progress(0) 33 | 34 | # These two elements will be filled in later, so we create a placeholder 35 | # for them using st.empty() 36 | frame_text = st.sidebar.empty() 37 | image = st.empty() 38 | 39 | m, n, s = 960, 640, 400 40 | x = np.linspace(-m / s, m / s, num=m).reshape((1, m)) 41 | y = np.linspace(-n / s, n / s, num=n).reshape((n, 1)) 42 | 43 | for frame_num, a in enumerate(np.linspace(0.0, 4 * np.pi, 100)): 44 | # Here were setting value for these two elements. 45 | progress_bar.progress(frame_num) 46 | frame_text.text("Frame %i/100" % (frame_num + 1)) 47 | 48 | # Performing some fractal wizardry. 49 | c = separation * np.exp(1j * a) 50 | Z = np.tile(x, (n, 1)) + 1j * np.tile(y, (1, m)) 51 | C = np.full((n, m), c) 52 | M: Any = np.full((n, m), True, dtype=bool) 53 | N = np.zeros((n, m)) 54 | 55 | for i in range(iterations): 56 | Z[M] = Z[M] * Z[M] + C[M] 57 | M[np.abs(Z) > 2] = False 58 | N[M] = i 59 | 60 | # Update the image placeholder by calling the image() function on it. 61 | image.image(1.0 - (N / N.max()), use_column_width=True) 62 | 63 | # We clear elements by calling empty on them. 64 | progress_bar.empty() 65 | frame_text.empty() 66 | 67 | # Streamlit widgets automatically run the script from top to bottom. Since 68 | # this button is not connected to any other logic, it just causes a plain 69 | # rerun. 70 | st.button("Re-run") 71 | 72 | 73 | st.set_page_config(page_title="Animation Demo", page_icon="📹") 74 | st.markdown("# Animation Demo") 75 | st.sidebar.header("Animation Demo") 76 | st.write( 77 | """This app shows how you can use Streamlit to build cool animations. 78 | It displays an animated fractal based on the the Julia Set. Use the slider 79 | to tune different parameters.""" 80 | ) 81 | 82 | animation_demo() 83 | 84 | show_code(animation_demo) 85 | -------------------------------------------------------------------------------- /pages/1_Plotting_Demo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import time 16 | 17 | import numpy as np 18 | 19 | import streamlit as st 20 | from streamlit.hello.utils import show_code 21 | 22 | 23 | def plotting_demo(): 24 | progress_bar = st.sidebar.progress(0) 25 | status_text = st.sidebar.empty() 26 | last_rows = np.random.randn(1, 1) 27 | chart = st.line_chart(last_rows) 28 | 29 | for i in range(1, 101): 30 | new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0) 31 | status_text.text("%i%% Complete" % i) 32 | chart.add_rows(new_rows) 33 | progress_bar.progress(i) 34 | last_rows = new_rows 35 | time.sleep(0.05) 36 | 37 | progress_bar.empty() 38 | 39 | # Streamlit widgets automatically run the script from top to bottom. Since 40 | # this button is not connected to any other logic, it just causes a plain 41 | # rerun. 42 | st.button("Re-run") 43 | 44 | 45 | st.set_page_config(page_title="Plotting Demo", page_icon="📈") 46 | st.markdown("# Plotting Demo") 47 | st.sidebar.header("Plotting Demo") 48 | st.write( 49 | """This demo illustrates a combination of plotting and animation with 50 | Streamlit. We're generating a bunch of random numbers in a loop for around 51 | 5 seconds. Enjoy!""" 52 | ) 53 | 54 | plotting_demo() 55 | 56 | show_code(plotting_demo) 57 | -------------------------------------------------------------------------------- /pages/2_Mapping_Demo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from urllib.error import URLError 16 | 17 | import pandas as pd 18 | import pydeck as pdk 19 | 20 | import streamlit as st 21 | from streamlit.hello.utils import show_code 22 | 23 | 24 | def mapping_demo(): 25 | @st.cache_data 26 | def from_data_file(filename): 27 | url = ( 28 | "https://raw.githubusercontent.com/streamlit/" 29 | "example-data/master/hello/v1/%s" % filename 30 | ) 31 | return pd.read_json(url) 32 | 33 | try: 34 | ALL_LAYERS = { 35 | "Bike Rentals": pdk.Layer( 36 | "HexagonLayer", 37 | data=from_data_file("bike_rental_stats.json"), 38 | get_position=["lon", "lat"], 39 | radius=200, 40 | elevation_scale=4, 41 | elevation_range=[0, 1000], 42 | extruded=True, 43 | ), 44 | "Bart Stop Exits": pdk.Layer( 45 | "ScatterplotLayer", 46 | data=from_data_file("bart_stop_stats.json"), 47 | get_position=["lon", "lat"], 48 | get_color=[200, 30, 0, 160], 49 | get_radius="[exits]", 50 | radius_scale=0.05, 51 | ), 52 | "Bart Stop Names": pdk.Layer( 53 | "TextLayer", 54 | data=from_data_file("bart_stop_stats.json"), 55 | get_position=["lon", "lat"], 56 | get_text="name", 57 | get_color=[0, 0, 0, 200], 58 | get_size=10, 59 | get_alignment_baseline="'bottom'", 60 | ), 61 | "Outbound Flow": pdk.Layer( 62 | "ArcLayer", 63 | data=from_data_file("bart_path_stats.json"), 64 | get_source_position=["lon", "lat"], 65 | get_target_position=["lon2", "lat2"], 66 | get_source_color=[200, 30, 0, 160], 67 | get_target_color=[200, 30, 0, 160], 68 | auto_highlight=True, 69 | width_scale=0.0001, 70 | get_width="outbound", 71 | width_min_pixels=3, 72 | width_max_pixels=30, 73 | ), 74 | } 75 | st.sidebar.markdown("### Map Layers") 76 | selected_layers = [ 77 | layer 78 | for layer_name, layer in ALL_LAYERS.items() 79 | if st.sidebar.checkbox(layer_name, True) 80 | ] 81 | if selected_layers: 82 | st.pydeck_chart( 83 | pdk.Deck( 84 | map_style=None, 85 | initial_view_state={ 86 | "latitude": 37.76, 87 | "longitude": -122.4, 88 | "zoom": 11, 89 | "pitch": 50, 90 | }, 91 | layers=selected_layers, 92 | ) 93 | ) 94 | else: 95 | st.error("Please choose at least one layer above.") 96 | except URLError as e: 97 | st.error( 98 | """ 99 | **This demo requires internet access.** 100 | Connection error: %s 101 | """ 102 | % e.reason 103 | ) 104 | 105 | 106 | st.set_page_config(page_title="Mapping Demo", page_icon="🌍") 107 | st.markdown("# Mapping Demo") 108 | st.sidebar.header("Mapping Demo") 109 | st.write( 110 | """This demo shows how to use 111 | [`st.pydeck_chart`](https://docs.streamlit.io/library/api-reference/charts/st.pydeck_chart) 112 | to display geospatial data.""" 113 | ) 114 | 115 | mapping_demo() 116 | 117 | show_code(mapping_demo) 118 | -------------------------------------------------------------------------------- /pages/3_DataFrame_Demo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | from urllib.error import URLError 16 | 17 | import altair as alt 18 | import pandas as pd 19 | 20 | import streamlit as st 21 | from streamlit.hello.utils import show_code 22 | 23 | 24 | def data_frame_demo(): 25 | @st.cache_data 26 | def get_UN_data(): 27 | AWS_BUCKET_URL = "https://streamlit-demo-data.s3-us-west-2.amazonaws.com" 28 | df = pd.read_csv(AWS_BUCKET_URL + "/agri.csv.gz") 29 | return df.set_index("Region") 30 | 31 | try: 32 | df = get_UN_data() 33 | countries = st.multiselect( 34 | "Choose countries", list(df.index), ["China", "United States of America"] 35 | ) 36 | if not countries: 37 | st.error("Please select at least one country.") 38 | else: 39 | data = df.loc[countries] 40 | data /= 1000000.0 41 | st.write("### Gross Agricultural Production ($B)", data.sort_index()) 42 | 43 | data = data.T.reset_index() 44 | data = pd.melt(data, id_vars=["index"]).rename( 45 | columns={"index": "year", "value": "Gross Agricultural Product ($B)"} 46 | ) 47 | chart = ( 48 | alt.Chart(data) 49 | .mark_area(opacity=0.3) 50 | .encode( 51 | x="year:T", 52 | y=alt.Y("Gross Agricultural Product ($B):Q", stack=None), 53 | color="Region:N", 54 | ) 55 | ) 56 | st.altair_chart(chart, use_container_width=True) 57 | except URLError as e: 58 | st.error( 59 | """ 60 | **This demo requires internet access.** 61 | Connection error: %s 62 | """ 63 | % e.reason 64 | ) 65 | 66 | 67 | st.set_page_config(page_title="DataFrame Demo", page_icon="📊") 68 | st.markdown("# DataFrame Demo") 69 | st.sidebar.header("DataFrame Demo") 70 | st.write( 71 | """This demo shows how to use `st.write` to visualize Pandas DataFrames. 72 | (Data courtesy of the [UN Data Explorer](http://data.un.org/Explorer.aspx).)""" 73 | ) 74 | 75 | data_frame_demo() 76 | 77 | show_code(data_frame_demo) 78 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | altair 2 | numpy 3 | pandas 4 | pydeck 5 | streamlit -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import inspect 16 | import textwrap 17 | 18 | import streamlit as st 19 | 20 | 21 | def show_code(demo): 22 | """Showing the code of the demo.""" 23 | show_code = st.sidebar.checkbox("Show code", True) 24 | if show_code: 25 | # Showing the code of the demo. 26 | st.markdown("## Code") 27 | sourcelines, _ = inspect.getsourcelines(demo) 28 | st.code(textwrap.dedent("".join(sourcelines[1:]))) 29 | --------------------------------------------------------------------------------