#1 Streamlit Magic Cheat?Sheets
Tushar Aggarwal
Head of Community @ Omdena, World's Largest Community First Grassroots AI Organization | Paris Lead| AI Product Manager| Environmentalist | MSc INSEEC,PARIS|RNCP Manager Level 7
All of Streamlit Jam-Packed into a Single Streamlit App (More than 17000 lines of Streamlit)
Introducing Streamlit Magic Cheat Sheets: The Streamlit application that unravels the mystical world of Streamlit.
Features:
Visual Appealing Application
Code Sample and Live View
Language Support: Easily switch between English, French, and German
Upcoming: More languages and Users tips
All most commonly used Streamlit codes in single page.
Write and?Magic
Streamlit has two easy ways to display information into your app: st.write and magic commands.
Text Elements
You can start streamlit applications with st.title to set the app's title(Text larger than Header size text). After that, there are 2 heading levels(lower sizes) you can use: st.header and st.subheader.
Any text can be entered with st.text, and Markdown with st.markdown(Uses Markdown language for text output).
Note st.write is versatile?, it can accepts multiple arguments, and multiple data types.
Data Elements
Streamlit applications can display data via Charts, DataFrames and Raw form.
Chart Elements
Streamlit applications supports several different charting libraries.
This include Matplotlib, Vega Lite & deck.gl.
Some native streamlit includes st.line_chart and st.area_chart.
Input Widgtes
Streamlit applications allows you to bake interactivity directly with buttons, sliders, text inputs, and more..
Media Elements
Streamlit Applications allows easy embeding of images, videos, and audio files directly.
Layouts and Containers
Streamlit Applications allows different layout built in and with Custom CSS, you can make yours too.
Chat Elements
Currently popular, Chat elements allows to buils conversations applications.
st.chat_message inserts a chat message container into the app so you can display messages from the user or the app.(Input container for Chats application)
Note st.chat_input allows to enter message that is (Input element for text for Chats application)
Where as st.status display output from long-running processes and external API calls
Status Elements
Streamlit applications has status elements to display progress bars, status messages (like warnings), and celebratory balloons.
Control Flow
Change execution allows to handle control flow in your applications(By defaul).
Group multiple widgets?—?allows the widgets is filled before rerunning the script to provide better UI experience.
Utilities
Streamlit Applications can have utilites like Placeholders, help, and options.
Mutate Charts
Allows to modify chart or dataframe as the app runs.
Using st.empty to replace a single element.
Using st.container or st.columns to replace multiple elements.
Using add_rows to append data to specific types of elements
State Management
With Session State, sharing variables between reruns, for each user session is possible.
Also has ability to manipulate state using Callbacks.
领英推荐
Performance
Streamlit provides cache primitives for data and global resources. They allow your app to stay performant even when loading data from the web, manipulating large datasets, or performing expensive computations.
Personalization
Updates and modifications, Context-dependent behavior and more.
Connections and Databases
You can start connect to a data store or API, or return an existing one.
App Menu
Streamlit provides a configurable menu within your app to access convenient tools for developers and viewers. By default, you can access developer options from the app menu when viewing an app locally or on Streamlit Community Cloud while logged into an account with administrative access. While viewing an app, click the icon in the upper-right corner to access the menu.
Button Behavior
Buttons created with st.button do not retain state. They return True on the script rerun resulting from their click and immediately return to False on the next script rerun. If a displayed element is nested inside if st.button(‘Click me’):, the element will be visible when the button is clicked and disappear as soon as the user takes their next action. This is because the script reruns and the button return value becomes False.
Caching
Streamlit runs your script from top to bottom at every user interaction or code change. This execution model makes development super easy. But it comes with two major challenges:
Command Line?Options
The purpose of this tool is to run Streamlit apps, change Streamlit configuration options, and help you diagnose and fix issues.
Configuration
Streamlit provides four different ways to set configuration options. This list is in reverse order of precedence, i.e. command line flags take precedence over environment variables when the same configuration option is provided multiple times.
Theming
Streamlit themes are defined using regular config options: a theme can be set via command line flag when starting your app using streamlit run or by defining it in the [theme] section of a?.streamlit/config.toml file. For more information on setting config options, please refer to the Streamlit configuration documentation.
The following config options show the default Streamlit Light theme recreated in the [theme] section of a?.streamlit/config.toml file.
Connecting To?Data
Most Streamlit apps need some kind of data or API access to be useful?—?either retrieving data to view or saving the results of some user action. This data or API is often part of some remote service, database, or other data source.
Streamlit provides st.connection() to more easily connect your Streamlit apps to data and APIs with just a few lines of code. This page provides a basic example of using the feature and then focuses on advanced usage.
Dataframes
Dataframes are a great way to display and edit data in a tabular format. Working with Pandas DataFrames and other tabular data structures is key to data science workflows. If developers and data scientists want to display this data in Streamlit, they have multiple options: st.dataframe and st.data_editor. If you want to solely display data in a table-like UI, st.dataframe is the way to go. If you want to interactively edit data, use st.data_editor. We explore the use cases and advantages of each option in the following sections.
Forms
When you don’t want to rerun your script with each input made by a user, st.form is here to help! Forms make it easy to batch user input into a single rerun. This guide to using forms provides examples and explains how users interact with forms.
Add Statefulness to?Apps
Streamlit app in a browser tab as a session. For each browser tab that connects to the Streamlit server, a new session is created. Streamlit reruns your script from top to bottom every time you interact with your app. Each reruns takes place in a blank slate: no variables are shared between runs.
Session State is a way to share variables between reruns, for each user session. In addition to the ability to store and persist state, Streamlit also exposes the ability to manipulate state using Callbacks. Session state also persists across apps inside a multipage app.
Widget Behavior
Widgets (like st.button, st.selectbox, and st.text_input) are at the heart of Streamlit apps. They are the interactive elements of Streamlit that pass information from your users into your Python code. Widgets are magical and often work how you want, but they can have surprising behavior in some situations. Understanding the different parts of a widget and the precise order in which events occur helps you achieve your desired results.
Working With Timezones
In general, working with timezones can be tricky. Your Streamlit app users are not necessarily in the same timezone as the server running your app. It is especially true of public apps, where anyone in the world (in any timezone) can access your app. As such, it is crucial to understand how Streamlit handles timezones, so you can avoid unexpected behavior when displaying datetime information.
Static File?Serving
Streamlit allows you to serve static files such as images, CSS, and JavaScript in your app. This guide will explore how to use the def static_file_serving(): function to serve static files and enhance the visual appearance of your app.
Https Support
Streamlit recommenda performing SSL termination in a reverse proxy or load balancer for self-hosted and production use cases, not directly in the app. Streamlit Community Cloud uses this approach, and every major cloud and app hosting platform should allow you to configure it and provide extensive documentation. You can find some of these platforms in our Deployment tutorials.
Secrets Management
Storing unencrypted secrets in a git repository is a bad practice. For applications that require access to sensitive credentials, the recommended solution is to store those credentials outside the repository?—?such as using a credentials file not committed to the repository or passing them as environment variables.
Security Reminders
Protect your secrets, Use environment variables, Keep?.gitignore updated, Pickle warning, and more.
Disclaimer: All trademarks, logos and brand names are the property of their respective owners (Streamlit Inc. & Snowflake Inc.). I am a fan Streamlit and this application is a guide/representation of official documentation and some tips.
Newsletter DataUnboxed
Github ?, LinkedIn , X , Hashnode, Buy me a beer
In 2023 I developed 300+ production ready applications on using various tools for client’s MVP, for guides and more, & I am going to be sharing 100+ of them for on various platforms…(Looking for business or affiliate collaborations? contact me!)
Follow me for more such applications Tushar Aggarwal