-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clones of demo provided by streamlit in Hello (#2)
In this PR, I recreated manually the demos made available by streamlit using **streamlit hello** 1. **bb0d973** File with list of tags 1. **c81226b** Added streamlit dependency 1. **bafcf4c** Configuration for the animation demo clone 1. **e0e6a3d** Added first two components of animation demo clone: `iteration` and `separation` 1. **912f25d** Added non-interactive progress bar 1. **c8f0b1b** Added two other components that are still invisible: - `frame_text` - `image` 1. **3464890** Added actual code that generates the animation 1. **2e3c281** Added missing imports in original source code 1. **68912f4** Added logic to rerun automatically 1. **bf3ba50** Modifying behavior for non-stop execution 1. **94242fc** Original code for plotting demo clone 1. **13c63d2** Moved button for re-run above the chart 1. **4ac1f52** Modified plotting demo to enable continuous regeneration of chart without interaction 1. **018597c** Code for the mapping demo that uses pydeck for mapping 1. **a9fe7cd** Code for data frame clone
- Loading branch information
Showing
16 changed files
with
6,146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# About | ||
A clone of Animation demo from streamlit built-in Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" Clone of animation demo from streamlit project""" | ||
from typing import Any | ||
import numpy as np | ||
import streamlit as st # Missing from code in streamlit's code | ||
|
||
# Streamlit widgets automatically run the script from top to bottom. Since | ||
# this button is not connected to any other logic, it just causes a plain | ||
# rerun. | ||
st.button("Re-run") | ||
|
||
# Interactive Streamlit elements, like these sliders, return their value. | ||
# This gives you an extremely simple interaction model. | ||
iterations = st.sidebar.slider("Level of detail", 2, 20, 10, 1) | ||
separation = st.sidebar.slider("Separation", 0.7, 2.0, 0.7885) | ||
|
||
# Non-interactive elements return a placeholder to their location | ||
# in the app. Here we're storing progress_bar to update it later. | ||
progress_bar = st.sidebar.progress(0) | ||
|
||
# These two elements will be filled in later, so we create a placeholder | ||
# for them using st.empty() | ||
frame_text = st.sidebar.empty() | ||
image = st.empty() | ||
|
||
m, n, s = 960, 640, 400 | ||
x = np.linspace(-m / s, m / s, num=m).reshape((1, m)) | ||
y = np.linspace(-n / s, n / s, num=n).reshape((n, 1)) | ||
|
||
while True: | ||
for frame_num, a in enumerate(np.linspace(0.0, 4 * np.pi, 100)): | ||
# Here were setting value for these two elements. | ||
progress_bar.progress(frame_num) | ||
frame_text.text("Frame %i/100" % (frame_num + 1)) | ||
|
||
# Performing some fractal wizardry. | ||
c = separation * np.exp(1j * a) | ||
Z = np.tile(x, (n, 1)) + 1j * np.tile(y, (1, m)) | ||
C = np.full((n, m), c) | ||
M: Any = np.full((n, m), True, dtype=bool) | ||
N = np.zeros((n, m)) | ||
|
||
for i in range(iterations): | ||
Z[M] = Z[M] * Z[M] + C[M] | ||
M[np.abs(Z) > 2] = False | ||
N[M] = i | ||
|
||
# Update the image placeholder by calling the image() function on it. | ||
image.image(1.0 - (N / N.max()), use_column_width=True) | ||
|
||
# We clear elements by calling empty on them. | ||
progress_bar.empty() | ||
frame_text.empty() |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[tool.poetry] | ||
name = "animation-demo-clone" | ||
version = "0.1.0" | ||
description = "A clone of streamlit Animation demo, available in built-in Hello project" | ||
authors = ["Dickson Souza <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
package-mode = false | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
streamlit = "^1.39.0" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" Clone of data frame demo""" | ||
import streamlit as st | ||
import pandas as pd | ||
from urllib.error import URLError | ||
import altair as alt | ||
|
||
@st.cache_data | ||
def get_UN_data(): | ||
AWS_BUCKET_URL = "https://streamlit-demo-data.s3-us-west-2.amazonaws.com" | ||
df = pd.read_csv(AWS_BUCKET_URL + "/agri.csv.gz") | ||
return df.set_index("Region") | ||
|
||
try: | ||
df = get_UN_data() | ||
countries = st.multiselect( | ||
"Choose countries", list(df.index), ["China", "United States of America"] | ||
) | ||
if not countries: | ||
st.error("Please select at least one country.") | ||
else: | ||
data = df.loc[countries] | ||
data /= 1000000.0 | ||
st.write("### Gross Agricultural Production ($B)", data.sort_index()) | ||
|
||
data = data.T.reset_index() | ||
data = pd.melt(data, id_vars=["index"]).rename( | ||
columns={"index": "year", "value": "Gross Agricultural Product ($B)"} | ||
) | ||
chart = ( | ||
alt.Chart(data) | ||
.mark_area(opacity=0.3) | ||
.encode( | ||
x="year:T", | ||
y=alt.Y("Gross Agricultural Product ($B):Q", stack=None), | ||
color="Region:N", | ||
) | ||
) | ||
st.altair_chart(chart, use_container_width=True) | ||
except URLError as e: | ||
st.error( | ||
""" | ||
**This demo requires internet access.** | ||
Connection error: %s | ||
""" | ||
% e.reason | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[tool.poetry] | ||
name = "dataframe-demo-clone" | ||
version = "0.1.0" | ||
description = "A clone of data frame demo" | ||
authors = ["Dickson Souza <[email protected]>"] | ||
license = "MIT" | ||
readme = "README.md" | ||
package-mode = false | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
streamlit = "^1.39.0" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4c3ff8a200218aa7f33d13c90283a9d584684617 refs/tags/Initial-Setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" A clone for mapping demo""" | ||
import streamlit as st | ||
import pandas as pd | ||
|
||
from urllib.error import URLError | ||
import pydeck as pdk | ||
|
||
@st.cache_data | ||
def from_data_file(filename): | ||
url = ( | ||
"https://raw.githubusercontent.com/streamlit/" | ||
"example-data/master/hello/v1/%s" % filename | ||
) | ||
return pd.read_json(url) | ||
|
||
|
||
try: | ||
ALL_LAYERS = { | ||
"Bike Rentals": pdk.Layer( | ||
"HexagonLayer", | ||
data=from_data_file("bike_rental_stats.json"), | ||
get_position=["lon", "lat"], | ||
radius=200, | ||
elevation_scale=4, | ||
elevation_range=[0, 1000], | ||
extruded=True, | ||
), | ||
"Bart Stop Exits": pdk.Layer( | ||
"ScatterplotLayer", | ||
data=from_data_file("bart_stop_stats.json"), | ||
get_position=["lon", "lat"], | ||
get_color=[200, 30, 0, 160], | ||
get_radius="[exits]", | ||
radius_scale=0.05, | ||
), | ||
"Bart Stop Names": pdk.Layer( | ||
"TextLayer", | ||
data=from_data_file("bart_stop_stats.json"), | ||
get_position=["lon", "lat"], | ||
get_text="name", | ||
get_color=[0, 0, 0, 200], | ||
get_size=10, | ||
get_alignment_baseline="'bottom'", | ||
), | ||
"Outbound Flow": pdk.Layer( | ||
"ArcLayer", | ||
data=from_data_file("bart_path_stats.json"), | ||
get_source_position=["lon", "lat"], | ||
get_target_position=["lon2", "lat2"], | ||
get_source_color=[200, 30, 0, 160], | ||
get_target_color=[200, 30, 0, 160], | ||
auto_highlight=True, | ||
width_scale=0.0001, | ||
get_width="outbound", | ||
width_min_pixels=3, | ||
width_max_pixels=30, | ||
), | ||
} | ||
st.sidebar.markdown("### Map Layers") | ||
selected_layers = [ | ||
layer | ||
for layer_name, layer in ALL_LAYERS.items() | ||
if st.sidebar.checkbox(layer_name, True) | ||
] | ||
if selected_layers: | ||
st.pydeck_chart( | ||
pdk.Deck( | ||
map_style=None, | ||
initial_view_state={ | ||
"latitude": 37.76, | ||
"longitude": -122.4, | ||
"zoom": 11, | ||
"pitch": 50, | ||
}, | ||
layers=selected_layers, | ||
) | ||
) | ||
else: | ||
st.error("Please choose at least one layer above.") | ||
except URLError as e: | ||
st.error( | ||
""" | ||
**This demo requires internet access.** | ||
Connection error: %s | ||
""" | ||
% e.reason | ||
) |
Oops, something went wrong.