0% found this document useful (0 votes)
6 views8 pages

Streamlit Intro

Uploaded by

ejodamen33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Streamlit Intro

Uploaded by

ejodamen33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Streamlit

Streamlit is a Python UI library that allows you to make interfaced very quickly.

In just a few minutes we can build entire websites using Streamlit.

We will be going through all the types of graphical components such as forms, modals, charts, buttons,
etc.

Virtual Environment
A Python virtual environment is an isolated directory containing its own Python interpreter and a
dedicated space for installing Python packages, separate from the system-wide Python installation.

This isolation prevents conflicts between different projects that may require different versions of the
same package.

Key aspects of Python virtual environments


Isolation
Each virtual environment is self-contained, ensuring that packages installed within one environment do
not interfere with packages in other environments or the global Python installation.

Dependency Management
Virtual environments allow for precise control over project dependencies. Specific package versions can
be installed and managed for each project without affecting other projects.

Reproducibility
By isolating dependencies, virtual environments make it easier to share projects and ensure that they
run consistently across different machines. The exact environment can be recreated using a
[Link] file.

Common commands for managing virtual environments (using venv module)


Creating a virtual environment
python -m venv my_project_env

Activating the virtual environment

my_project_env\Scripts\[Link] => For Windows

source my_project_env/bin/activate => For Mac/Linux

Installing packages
pip install package_name

Deactivating the virtual environment


deactivate
Start Streamlit App
Create virtual environment named streamlit-course

Activate virtual environment

Install the following Python packages:

• streamlit
• pandas
• numpy
• matplotlib

create file named [Link] in project directory

In [Link], use the following:


[Link]()

[Link](): This function is used to add anything to a web app.

Save the file, then run the file in the terminal or command prompt using the following:
streamlit run [Link]

Text Elements
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()

Data Elements
• [Link]()
• st.data_editor()
• [Link]()

Chart Elements
• st.bar_chart()
• st.line_chart()
• st.scatter_chart()
• [Link]()
Form Elements
• [Link]()
• [Link]()
• st.form_submit_button()
• st.text_input()
• st.text_area()
• st.date_input()
• st.time_input()
• [Link]()
• [Link]()
• st.select_slider()
• [Link]()
• st.file_uploader()
• [Link]()

Session State
Common dataflow issue to note with Streamlit.

# Numbers
x = 10 # integer
y = 3.14 # float

# Strings
name = “Ada Lovelace”

# Boolean
is_ml_fun = True
Session state is something (like a dictionary) that we can use to store values within the same user
session.

import streamlit as st

if “counter” not in st.session_state:

st.session_state[“counter”] = 0

if [Link](“Increment Counter”):

st.session_state[“counter”] += 1

[Link](f”Counter incremented to {st.session_state[‘counter’]}”)

if [Link](“Reset”):

st.session_state[“counter”] = 0

else:

[Link](f”Counter did not reset”)

[Link](f“Counter value: {st.session_state[‘counter’]}”)


Callbacks
In Streamlit, a callback is simply a function that gets executed automatically when a widget’s value
changes or when a user interacts with that widget.

Streamlit lets you attach these callbacks to widgets using the on_change or on_click parameters. This is
useful when you want an action (like updating session state, running a calculation, or triggering some UI
change) to happen immediately after a user interaction, without requiring the user to click a separate
"submit" button.

Example 1: Using on_change with a text input


import streamlit as st

def update_name():

st.session_state[“greeting”] = f”Hello, {st.session_state.name}!”

st.text_input(“Enter your name”, key=”name”, on_change=update_name)

[Link](st.session_state.get(“greeting”, “”))

Here:

• When the user types a name, update_name() is called automatically.


• The function updates session state and the greeting appears.

Example 2: Using on_click with a button

import streamlit as st

def say_hello():

[Link](“Hello!”)

[Link](“Click me”, on_click=say_hello)

Here:

• When the button is clicked, the say_hello() function runs.


Key Points:
• on_change -> used with widgets like st.text_input, [Link], [Link], etc. (fires when the
widget’s value changes).
• on_click -> used with [Link] and st.form_submit_button (fires when clicked).
• Callbacks + st.session_state -> usually go hand in hand to store and update values persistently.

Layouts
Streamlit has a few elements for laying out components on a web page. These elements are responsive.

• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()
• [Link]()

Multi-Page Apps
Create a .py file for your home page – [Link]

import streamlit as st

[Link](“Home”)

Then create a directory (folder) called pages, in the same path, just beside [Link].

Inside the pages directory, create a file 1_Page1.py

import streamlit as st

[Link](“Page 1”)

Inside the pages directory, create a file 2_Page2.py

import streamlit as st

[Link](“Page 2”)
App structure

your_working_directory/
├── pages/
│ ├── 1_Page1.py
│ └── 2_Page2.py
└── [Link]

Run your multipage app just like you would for a single-page app.

streamlit run [Link]

Deploy Streamlit App


Study Material
• Streamlit Tutorials (34 Videos) – Coding is Fun
• Streamlit Mini Course – Tech With Tim
• Streamlit Multipage Documentation
• Streamlit Documentation

You might also like