2023-02-10

Streamlit

What is Streamlit

Streamlit is a Python framework that allows for easy development of data science and machine learning applications without requiring knowledge of HTML, CSS, or other web technologies. It enables data visualization without the need for complex web development.

Installation

You can install Streamlit using the following command:

bash
$ pip install streamlit

Running Streamlit

Create an app.py file with the following content:

app.py
import streamlit as st

st.title("hello world")

Then, execute the following command to run the Streamlit app:

bash
$ streamlit run app.py
You can now view your Streamlit app in your browser.

Local URL: http://localhost:8555

You can access your Streamlit application by visiting http://localhost:8555 in your web browser.

Streamlit | 1

Text

With Streamlit, you can display text using Markdown and other text-related functions.

app.py
import streamlit as st

st.title("title") # Title
st.header("header") # Header
st.write("write") # Display
st.markdown("# markdown") # markdown
st.text("text") # Text

Streamlit | 2

Widgets

Streamlit provides various interactive widgets like sliders, checkboxes, radio buttons, etc.

app.py
import streamlit as st

st.button("Button")  # Button
st.selectbox("Selectbox", ("Option 1", "Option 2"))  # Select box
st.multiselect("Multi selectbox", ("Option 1", "Option 2"))  # Multi select box
st.slider("Slider", 0, 100, 20)  # Slider
st.radio("Radio button", ("Radio 1", "Radio 2"))  # Radio button
st.checkbox("Check button")  # Checkbox
st.text_input("Text input")  # Text input (one line)
st.text_area("Text area")  # Text input (multiple line)
st.file_uploader("Choose file")  # File upload

Streamlit | 3

You can control the display of widgets by assigning their return values to variables.

app.py
import streamlit as st

checked = st.checkbox("Check button")

if checked:
    st.button("Button")

Streamlit | 4
Streamlit | 5

Sidebar

In Streamlit, apart from the main application area, you can use the sidebar to display widgets.

app.py
import streamlit as st

st.button("Button")

st.sidebar.text_input("Text input")
st.sidebar.text_area("Text area")
st.sidebar.slider("Slider", 0, 100, 20)

Streamlit | 6

Table

Streamlit has built-in support for displaying Pandas DataFrames as formatted tables, making it easy to present data in tabular form.

app.py
import numpy as np
import pandas as pd

import streamlit as st

df = pd.DataFrame(np.random.randn(5, 10))
st.dataframe(df)  # Pandas dataframe
st.table(df)  # Table
st.metric(
    label="Temperature",
    value=f"{round(df[0].mean(), 2)} °F",
    delta=f"{round(df[0].max() - df[0].min(), 2)} °F",
)  # Metrics

Streamlit | 7

Graphs

You can use Streamlit to visualize data through graphs.

app.py
import streamlit as st
import pandas as pd
import numpy as np

dataframe = pd.DataFrame(np.random.randn(20,3), columns=["a", "b", "c"])
st.line_chart(dataframe) # Line
st.area_chart(dataframe) # Chart
st.bar_chart(dataframe) # Bar

Streamlit | 8

Maps

Streamlit provides built-in support for displaying geographical data.

app.py
import streamlit as st
import pandas as pd
import numpy as np

df = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
    columns=['lat', 'lon'])
st.map(df)

Streamlit | 9

References

https://docs.streamlit.io/
https://docs.streamlit.io/library/api-reference

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!