Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Data Visualization with Streamlit

Avatar for Luca Corbucci Luca Corbucci
June 11, 2022
230

Data Visualization with Streamlit

Avatar for Luca Corbucci

Luca Corbucci

June 11, 2022
Tweet

More Decks by Luca Corbucci

Transcript

  1. • 👨💻 Ph.D. Student @ University of Pisa • 🎤

    Podcaster @ PointerPodcast • 🦸 Community Manager @ SuperHeroesValley • 🌏 lucacorbucci.me 👋 Hi, I’m Luca @lucacorbucci
  2. What is Streamlit? • It is an open source framework

    for data scientist and ML engineer • It allows you to develop interfaces for ML projects • It allows you to write your interface using Python • It is easy to use: you don’t need any previous frontend experience • It o ff ers an easy way to deploy your projects
  3. import streamlit as st import pandas as pd st.title("Hello Pycon

    IT") df = pd.read_csv("./race-winners.csv") st.dataframe(df)
  4. import streamlit as st import pandas as pd st.title("Hello Pycon

    IT") df = pd.read_csv("./race-winners.csv") st.dataframe(df) df1 = df[df['Class'] == 'MotoGP™'] count = df1['Rider'].value_counts().head(20) st.bar_chart(count)
  5. import streamlit as st import pandas as pd st.title("Hello Pycon

    IT") df = pd.read_csv("./race-winners.csv") st.dataframe(df) class_type = st.radio( "Choose the class you want to display", ('Moto3™', 'Moto2™', 'MotoGP™')) df1 = df[df['Class'] == class_type] count = df1['Rider'].value_counts().head(20) st.bar_chart(count)
  6. import streamlit as st import pandas as pd st.title("Hello Pycon

    IT") df = pd.read_csv("./race-winners.csv") st.dataframe(df) class_type = st.radio( "Choose the class you want to display", ('Moto3™', 'Moto2™', 'MotoGP™')) df1 = df[df['Class'] == class_type] count = df1['Rider'].value_counts().head(20) st.bar_chart(count) option = st.selectbox( 'Select the year you want to display:', (i for i in range(1949, 2023))) df1 = df1[df1['Season'] == option] count = df1['Rider'].value_counts().head(20) st.bar_chart(count)
  7. @st.cache is our friend • We don’t want to execute

    multiple times a slow code • We can mark functions with the decorator @st.cache • It allows us to reuse data avoiding executing slow code multiple times def load_data(path: str) -> pd.DataFrame: data = pd.read_csv(path) return data df = load_data("./race-winners.csv") st.dataframe(df)
  8. @st.cache is our friend • We don’t want to execute

    multiple times a slow code • We can mark functions with the decorator @st.cache • It allows us to reuse data avoiding executing slow code multiple times @st.cache def load_data(path: str) -> pd.DataFrame: data = pd.read_csv(path) return data df = load_data("./race-winners.csv") st.dataframe(df)
  9. @st.cache in details • Once we add the decorator, Streamlit

    will check: • If the input parameters changed • If the body of the function changed • If the body of the functions called inside the function changed • If the variables used inside the function changed • Streamlit stores the data in a local cache, if any of these components changed then it will execute again the function otherwise it will use the cached data
  10. Deploy your first data app • Streamlit allows the deployment

    of the data apps on Streamlit Cloud • Streamlit Cloud is connected with a GitHub repo and it is able to deploy your app • It is a free service (with some limitations)