Aller au contenu

Streamlit

Streamlit est un framework open-source pour créer des applications web interactives en Python qui sont orientée data.

Installation et usage

  • pip install streamlit (ou ajouter à votre requirements.txt)
  • Créer un fichier app.py et y écrire du code Streamlit (voir exemples ci-dessous)
  • Lancer l'application avec la commande streamlit run app.py

Quelques exemples

Tour rapide
import streamlit as st
import pandas as pd
import numpy as np

st.title("Hello streamlit")


titanic = pd.read_csv("titanic.csv")

fare_to_filter = st.slider(
    "Max Fare", titanic.Fare.min(), titanic["Fare"].max())
titanic = titanic[titanic["Fare"] < fare_to_filter]

tabs = st.tabs(["Dataframe", "Histogram"])
tabs[0].subheader('Raw data')
tabs[0].write(titanic)

tabs[1].subheader('Number of passengers per class')
hist_values = np.histogram(titanic["Pclass"], bins=3, range=(1, 3))[0]
tabs[1].bar_chart(hist_values)
lanchain avec google AI
import streamlit as st
from langchain_google_genai import ChatGoogleGenerativeAI

system_message = (
    "system", """You are a weather specialist. 
    The human provides humidity and temperature. 
    Can you explain the results and comment on the air condition in the house.
    Please provide a short answer in two sentences.
    """)
googleai_api_key = st.sidebar.text_input("Google AI API Key", type="password")

# Arrêter l'application streamlit si pas de clé d'API
if len(googleai_api_key) == 0:
    st.error("No API key")
    st.stop()

st.title("AI weather analyst")
llm = ChatGoogleGenerativeAI(
    model="gemini-2.0-flash-lite", api_key=googleai_api_key)

with st.form("my_form"):
    text = st.text_area("Prompt:")
    submitted = st.form_submit_button("Submit")

if submitted:
    human_message = ("human", text)
    response = llm.invoke([system_message, human_message])
    st.info(response.content)

Déployer une application grâce à Streamlit Cloud

Streamlit Cloud est une plateforme de déploiement d'applications Streamlit gratuite.

  • Déployer le code de votre application sur GitHub
  • Créer un compte sur Streamlit Cloud
  • Connecter votre compte GitHub à Streamlit Cloud
  • Depuis Streamlit Cloud, créer une nouvelle application en sélectionnant le dépôt GitHub contenant votre code