Slide 1

Slide 1 text

Data Visualization With Streamlit Luca Corbucci PyCon Italia - Firenze 04/06/2022

Slide 2

Slide 2 text

• 👨💻 Ph.D. Student @ University of Pisa • 🎤 Podcaster @ PointerPodcast • 🦸 Community Manager @ SuperHeroesValley • 🌏 lucacorbucci.me 👋 Hi, I’m Luca @lucacorbucci

Slide 3

Slide 3 text

Once upon a time, at the end of a university project…

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

How could we report our results without developing a dashboard?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Installation

Slide 10

Slide 10 text

Let’s create our first app

Slide 11

Slide 11 text

import streamlit as st import pandas as pd

Slide 12

Slide 12 text

import streamlit as st import pandas as pd st.title("Hello Pycon IT")

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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)

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

@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)

Slide 18

Slide 18 text

@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)

Slide 19

Slide 19 text

@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

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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)

Slide 22

Slide 22 text

Deploy your first data app

Slide 23

Slide 23 text

Demo!

Slide 24

Slide 24 text

Thank you @lucacorbucci 🌏 lucacorbucci.me 👨💻 github.com/lucacorbucci/ 📥 [email protected]