From b72210e1564e13656941d76f48ff9f5e2e1648a2 Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Sun, 2 Feb 2025 13:16:12 -0800 Subject: [PATCH 1/3] Update hello app --- Hello.py | 56 +++++++------------ .../0_Animation_Demo.py => animation_demo.py | 22 ++++---- .../3_DataFrame_Demo.py => dataframe_demo.py | 26 ++++----- pages/2_Mapping_Demo.py => mapping_demo.py | 25 ++++----- pages/1_Plotting_Demo.py => plotting_demo.py | 21 ++++--- streamlit_app.py | 55 ++++++++++++++++++ utils.py | 2 +- 7 files changed, 118 insertions(+), 89 deletions(-) rename pages/0_Animation_Demo.py => animation_demo.py (84%) rename pages/3_DataFrame_Demo.py => dataframe_demo.py (76%) rename pages/2_Mapping_Demo.py => mapping_demo.py (85%) rename pages/1_Plotting_Demo.py => plotting_demo.py (74%) create mode 100644 streamlit_app.py diff --git a/Hello.py b/Hello.py index c0513b4..b27309e 100644 --- a/Hello.py +++ b/Hello.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,39 +13,25 @@ # limitations under the License. import streamlit as st -from streamlit.logger import get_logger -LOGGER = get_logger(__name__) - - -def run(): - st.set_page_config( - page_title="Hello", - page_icon="👋", - ) - - st.write("# Welcome to Streamlit! 👋") - - st.sidebar.success("Select a demo above.") - - st.markdown( - """ - Streamlit is an open-source app framework built specifically for - Machine Learning and Data Science projects. - **👈 Select a demo from the sidebar** to see some examples - of what Streamlit can do! - ### Want to learn more? - - Check out [streamlit.io](https://streamlit.io) - - Jump into our [documentation](https://docs.streamlit.io) - - Ask a question in our [community - forums](https://discuss.streamlit.io) - ### See more complex demos - - Use a neural net to [analyze the Udacity Self-driving Car Image - Dataset](https://github.com/streamlit/demo-self-driving) - - Explore a [New York City rideshare dataset](https://github.com/streamlit/demo-uber-nyc-pickups) +st.set_page_config(page_title="Hello", page_icon=":material/waving_hand:") +st.title("Welcome to Streamlit! 👋") +st.write( """ - ) - - -if __name__ == "__main__": - run() + Streamlit is an open-source app framework built specifically for + machine learning and data science projects. + **👈 Select a demo from the sidebar** to see some examples + of what Streamlit can do! + + ### Want to learn more? + - Check out [streamlit.io](https://streamlit.io) + - Jump into our [documentation](https://docs.streamlit.io) + - Ask a question in our [community + forums](https://discuss.streamlit.io) + + ### See more complex demos + - Use a neural net to [analyze the Udacity Self-driving Car Image + Dataset](https://github.com/streamlit/demo-self-driving) + - Explore a [New York City rideshare dataset](https://github.com/streamlit/demo-uber-nyc-pickups) + """ +) diff --git a/pages/0_Animation_Demo.py b/animation_demo.py similarity index 84% rename from pages/0_Animation_Demo.py rename to animation_demo.py index 72881cb..efaffd1 100644 --- a/pages/0_Animation_Demo.py +++ b/animation_demo.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ def animation_demo() -> None: - # 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) @@ -58,7 +57,7 @@ def animation_demo() -> None: 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) + image.image(1.0 - (N / N.max()), use_container_width=True) # We clear elements by calling empty on them. progress_bar.empty() @@ -67,18 +66,17 @@ def animation_demo() -> None: # 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") + st.button("Rerun") -st.set_page_config(page_title="Animation Demo", page_icon="📹") -st.markdown("# Animation Demo") -st.sidebar.header("Animation Demo") +st.set_page_config(page_title="Animation demo", page_icon=":material/animation:") +st.title("Animation demo") st.write( - """This app shows how you can use Streamlit to build cool animations. -It displays an animated fractal based on the the Julia Set. Use the slider -to tune different parameters.""" + """ + This app shows how you can use Streamlit to build cool animations. + It displays an animated fractal based on the the Julia Set. Use the slider + to tune different parameters. + """ ) - animation_demo() - show_code(animation_demo) diff --git a/pages/3_DataFrame_Demo.py b/dataframe_demo.py similarity index 76% rename from pages/3_DataFrame_Demo.py rename to dataframe_demo.py index fd8d9f8..a4dfa95 100644 --- a/pages/3_DataFrame_Demo.py +++ b/dataframe_demo.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -38,7 +38,8 @@ def get_UN_data(): else: data = df.loc[countries] data /= 1000000.0 - st.write("### Gross Agricultural Production ($B)", data.sort_index()) + st.subheader("Gross agricultural production ($B)") + st.dataframe(data.sort_index()) data = data.T.reset_index() data = pd.melt(data, id_vars=["index"]).rename( @@ -55,23 +56,16 @@ def get_UN_data(): ) st.altair_chart(chart, use_container_width=True) except URLError as e: - st.error( - """ - **This demo requires internet access.** - Connection error: %s - """ - % e.reason - ) + st.error(f"This demo requires internet access. Connection error: {e.reason}") -st.set_page_config(page_title="DataFrame Demo", page_icon="📊") -st.markdown("# DataFrame Demo") -st.sidebar.header("DataFrame Demo") +st.set_page_config(page_title="DataFrame demo", page_icon=":material/table:") +st.title("DataFrame demo") st.write( - """This demo shows how to use `st.write` to visualize Pandas DataFrames. -(Data courtesy of the [UN Data Explorer](http://data.un.org/Explorer.aspx).)""" + """ + This demo shows how to use `st.dataframe` to visualize a Pandas DataFrame. + Data courtesy of the [UN Data Explorer](http://data.un.org/Explorer.aspx). + """ ) - data_frame_demo() - show_code(data_frame_demo) diff --git a/pages/2_Mapping_Demo.py b/mapping_demo.py similarity index 85% rename from pages/2_Mapping_Demo.py rename to mapping_demo.py index 6e0e8d8..6179e1b 100644 --- a/pages/2_Mapping_Demo.py +++ b/mapping_demo.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ def from_data_file(filename): try: ALL_LAYERS = { - "Bike Rentals": pdk.Layer( + "Bike rentals": pdk.Layer( "HexagonLayer", data=from_data_file("bike_rental_stats.json"), get_position=["lon", "lat"], @@ -41,7 +41,7 @@ def from_data_file(filename): elevation_range=[0, 1000], extruded=True, ), - "Bart Stop Exits": pdk.Layer( + "Bart stop exits": pdk.Layer( "ScatterplotLayer", data=from_data_file("bart_stop_stats.json"), get_position=["lon", "lat"], @@ -49,7 +49,7 @@ def from_data_file(filename): get_radius="[exits]", radius_scale=0.05, ), - "Bart Stop Names": pdk.Layer( + "Bart stop names": pdk.Layer( "TextLayer", data=from_data_file("bart_stop_stats.json"), get_position=["lon", "lat"], @@ -58,7 +58,7 @@ def from_data_file(filename): get_size=10, get_alignment_baseline="'bottom'", ), - "Outbound Flow": pdk.Layer( + "Outbound flow": pdk.Layer( "ArcLayer", data=from_data_file("bart_path_stats.json"), get_source_position=["lon", "lat"], @@ -72,7 +72,7 @@ def from_data_file(filename): width_max_pixels=30, ), } - st.sidebar.markdown("### Map Layers") + st.sidebar.subheader("Map layers") selected_layers = [ layer for layer_name, layer in ALL_LAYERS.items() @@ -103,15 +103,12 @@ def from_data_file(filename): ) -st.set_page_config(page_title="Mapping Demo", page_icon="🌍") -st.markdown("# Mapping Demo") -st.sidebar.header("Mapping Demo") +st.set_page_config(page_title="Mapping demo", page_icon=":material/public:") +st.title("Mapping demo") st.write( - """This demo shows how to use -[`st.pydeck_chart`](https://docs.streamlit.io/library/api-reference/charts/st.pydeck_chart) -to display geospatial data.""" + """ + This demo shows how to use `st.pydeck_chart` to display geospatial data. + """ ) - mapping_demo() - show_code(mapping_demo) diff --git a/pages/1_Plotting_Demo.py b/plotting_demo.py similarity index 74% rename from pages/1_Plotting_Demo.py rename to plotting_demo.py index d321a9e..63966c9 100644 --- a/pages/1_Plotting_Demo.py +++ b/plotting_demo.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ def plotting_demo(): for i in range(1, 101): new_rows = last_rows[-1, :] + np.random.randn(5, 1).cumsum(axis=0) - status_text.text("%i%% Complete" % i) + status_text.text(f"{i}% complete") chart.add_rows(new_rows) progress_bar.progress(i) last_rows = new_rows @@ -39,18 +39,17 @@ def plotting_demo(): # 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") + st.button("Rerun") -st.set_page_config(page_title="Plotting Demo", page_icon="📈") -st.markdown("# Plotting Demo") -st.sidebar.header("Plotting Demo") +st.set_page_config(page_title="Plotting demo", page_icon=":material/show_chart:") +st.title("Plotting demo") st.write( - """This demo illustrates a combination of plotting and animation with -Streamlit. We're generating a bunch of random numbers in a loop for around -5 seconds. Enjoy!""" + """ + This demo illustrates a combination of plotting and animation with + Streamlit. We're generating a bunch of random numbers in a loop for around + 5 seconds. Enjoy! + """ ) - plotting_demo() - show_code(plotting_demo) diff --git a/streamlit_app.py b/streamlit_app.py new file mode 100644 index 0000000..b3f3850 --- /dev/null +++ b/streamlit_app.py @@ -0,0 +1,55 @@ +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from pathlib import Path + +import streamlit as st + +dir_path = Path(__file__).parent + + +# Note that this needs to be in a method so we can have an e2e playwright test. +def run(): + page = st.navigation( + [ + st.Page( + dir_path / "hello.py", title="Hello", icon=":material/waving_hand:" + ), + st.Page( + dir_path / "dataframe_demo.py", + title="DataFrame demo", + icon=":material/table:", + ), + st.Page( + dir_path / "plotting_demo.py", + title="Plotting demo", + icon=":material/show_chart:", + ), + st.Page( + dir_path / "mapping_demo.py", + title="Mapping demo", + icon=":material/public:", + ), + st.Page( + dir_path / "animation_demo.py", + title="Animation demo", + icon=":material/animation:", + ), + ] + ) + page.run() + + +if __name__ == "__main__": + run() diff --git a/utils.py b/utils.py index 20b27de..36f9000 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,4 @@ -# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022) +# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2025) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From db243ea9c23ec83015379f0d148fe43c65614a0b Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Sun, 2 Feb 2025 13:32:49 -0800 Subject: [PATCH 2/3] Rename Hello.py to hello.py --- Hello.py => hello.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Hello.py => hello.py (100%) diff --git a/Hello.py b/hello.py similarity index 100% rename from Hello.py rename to hello.py From e9b71b4fd3bf53efa74f55e23e0cff11e72c8340 Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Sun, 2 Feb 2025 15:53:13 -0800 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 035ebfe..1ead29a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ This is the app you get when you run `streamlit hello`, extracted as its own app. -Edit [Hello.py](./Hello.py) to customize this app to your heart's desire. ❤️ +Edit [streamlit_app.py](./streamlit_app.py) to customize this app to your heart's desire. ❤️ Check it out on [Streamlit Community Cloud](https://st-hello-app.streamlit.app/)