Visualizer #1 | Streamlit


Introduction



1. Basic Concepts

  • First you sprinkle a few Streamlit commands into a normal Python script, then you run it with streamlit run
  • The app is your canvas, where you’ll draw charts, text, widgets, tables, and more.

1.2 Data Flow

  • Streamlit apps have a unique data flow: any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom.
    • Whenever you modify your app’s source code.
    • Whenever a user interacts with widgets in the app

1.3 Display and style data

  •  You can pass almost anything to st.write(): text, data, Matplotlib figures, Altair charts, and more.

1.4 Widgets

1.5 Layout

  • Streamlit makes it easy to organize your widgets in a left panel sidebar with st.sidebar.

  • st.columns lets you place widgets side-by-side, and st.expander lets you conserve space by hiding away large content.



2. Advanced Concepts

  • Now that you know how a Streamlit app runs and handles data, let’s talk about being efficient.
    • Caching allows you to save the output of a function so you can skip over it on rerun.
    • Session State lets you save information for each user that is preserved between reruns.

2.1 Caching

  • The basic idea behind caching is to store the results of expensive function calls and return the cached result when the same inputs occur again. This avoids repeated execution of a function with the same input values.
    • st.cache_data is the recommended way to cache computations that return data.
    • st.cache_resource is the recommended way to cache global resources like ML models or database connections.

2.2 Session State

  • Session State provides a dictionary-like interface where you can save information that is preserved between script reruns.

2.3 Connections

  • st.connection takes care of the caching for you so you can enjoy fewer lines of code.


3. Additional Streamlit Features

  • Here’s a quick look at some extra features to take your app to the next level.

3.1 Theming

  • Streamlit supports Light and Dark themes out of the box.

  • Streamlit will first check if the user viewing an app has a Light or Dark mode preference set by their operating system and browser. If so, then that preference will be used.

3.2 Pages

  • As apps grow large, it becomes useful to organize them into multiple pages.

  • Streamlit provides a frictionless way to create multipage apps.

    • simply add new .py files in pages folder likepages/page1.py