Streamlit data visualization¶
Description¶
Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science. In just a few minutes you can build and deploy powerful data apps.
Installation¶
Streamlit can be installed with conda
(or [micro]mamba
) and pip
, as follows:
Download package:
pip install streamlit
Demo¶
streamlit hello
Key concepts¶
Running the app¶
Streamlit creates a local server as soon as you run the script with the following command:
streamlit run your_script.py [-- script args]
This command will open a new tab in your default web browser.
Build the app¶
Let's imagine that your_script.py is like a canvas. In this canvas you can build the components as you prefer.
Components
These components are necessary to structure the app. We have 5 layout components:
-
st.container
: It divides the canvas in containers -
st.sidebar
: It allows to add object to the sidebar. -
st.columns
: It divides the container or the canvas in columns. -
st.expander
: it adds an expander in the canva
In Streamlit there are several ways of writing text in the app:
st.text
: Write fixed-width and preformatted text.-
st.markdown
: string formatted as Markdown, it can also read html string (although it is considered unsafe) -
st.code
: renders code as text st.latex
: reads latex syntax, useful for math expressions.st.divider
: adds a horizontal line that divides the area of the container/column etcst.title
,st.header
,st.caption
,st.subheader
: self-explanatory.
Streamlit is optimize for visualize Data quickly, interactively, and from multiple different angles. You can use several functions:
- Data
st.dataframe
: it reads and renders a Pandas dataframest.metric
: it returns a KPI specified by the userst.table
: it returns a tablest.json
: it returns a box with a json that can be copied by the user at certain level.
Streamlite has also built in wrapper for several package used to plot dynamically the data, some of them are:
- Charts
st.line_chart
,st.area_chart
,st.bar_chart
st.pyplot
: wrapper of matplotlib pyplot.st.plotly
: wrapper of plotly, create interactive and dynamic plots.
Streamlit let you go through your data dynamically. This can be done through the use of button, selectors, several types of inputs or via file uploader.
st.button
: Simple button that return a Boolean (False, True) if clicked or not - can be used as a condition in if statements.st.selectbox
: Dropdown menu that can be selected. Returns the string of the option selected.st.slider
,st.checkbox
.
Streamlit allows to return messages to the user in some specific cases. i.e. if the user needs to produce a plot by selecting some data, the message should appear only if the selection is not made.
import streamlit as st
selection = st.selectbox("This is the title of the selectbox", ("Select Option", "Option0", "option1", "option2"))
if selection == "Select Option":
st.info("please select an option")
else:
st.markdown(
"""
You selected {option}
""".format(option=selection)
)
Types of message statuses:
st.info
: Returns a blue box with some info.st.error
: Returns a red box with error info.st.warning
: Returns a yellow box.st.success
: Returns a green box.
Examples¶
Documentation¶
Source¶
- Federica Brando