Streamlit Magic Cheat Sheets
encapsulates the Streamlit documentation version 1.28.0, providing concise summaries along with exemplar implementations of Streamlit code snippets.
VersionOctober, 2023 v1.1.1
The development and maintenance of this application are solely undertaken by Tushar Aggarwal
- ©2023 Tushar Aggarwal. All rights reserved
- Medium
- Tushar-Aggarwal.com
- X
- Data Unboxed Newsletter
- HuggingFace
- DagsHub
- Hashnode
- NovyPro
- New Kaggle
- Introduction to Streamlit
- Getting Started with Streamlit
- Streamlit Magic
- Streamlit Widgets
- Streamlit Data Display
- Streamlit Charts
- Streamlit Media
- Streamlit Layout
- Streamlit State Management
- Streamlit Caching
- Streamlit Theming
- Streamlit Deployment
- Streamlit Components
- Streamlit Best Practices
- Streamlit Resources
- Streamlit Examples
- Streamlit Data Science Examples
- Streamlit Machine Learning Examples
- Streamlit NLP Examples
- Streamlit Computer Vision Examples
- Streamlit Recommender Systems Examples
- Streamlit Finance Examples
- Streamlit Healthcare Examples
- Streamlit Retail Examples
- Streamlit Cybersecurity Examples
- Streamlit IoT Examples
- Streamlit Alternatives
- Streamlit Integrations
- Streamlit Contributions
- Streamlit FAQ
- Streamlit Glossary
- Streamlit Cheat Sheet
- Streamlit Tips and Tricks
- Streamlit Roadmap
- Streamlit Changelog
- Streamlit License
- Streamlit Contributing
- Streamlit Code of Conduct
- Streamlit Security
- Streamlit Feedback
Streamlit is an open-source Python library that allows you to create interactive web applications for data science and machine learning projects with minimal effort. It provides a simple and intuitive way to build user interfaces, visualize data, and deploy models without the need for extensive web development knowledge.
Streamlit's main goal is to bridge the gap between data scientists and web development, enabling them to share their work and insights with others in a user-friendly and interactive manner. With Streamlit, you can create interactive dashboards, data explorers, and model visualizations using pure Python code.
Some key features of Streamlit include:
- Easy to Learn: Streamlit has a simple and intuitive API, making it easy for data scientists and developers to get started with building web applications.
- Interactive Widgets: Streamlit provides a wide range of interactive widgets, such as sliders, dropdowns, and text inputs, allowing users to interact with your application and explore data.
- Data Visualization: Streamlit supports various data visualization libraries, including Matplotlib, Plotly, Altair, and more, enabling you to create beautiful and interactive visualizations.
- Caching and State Management: Streamlit provides built-in caching and state management capabilities, allowing you to create stateful applications and improve performance.
- Deployment: Streamlit applications can be easily deployed to various platforms, including Streamlit Sharing, Heroku, AWS, and more.
Streamlit has gained significant popularity in the data science and machine learning communities due to its simplicity and ease of use. It allows data scientists to focus on their core tasks, such as data analysis and model development, while providing a seamless way to share their work with others.
To get started with Streamlit, you need to have Python installed on your system. You can install Streamlit using pip, the Python package installer:
pip install streamlit
Alternatively, you can install Streamlit in a virtual environment to keep your project dependencies isolated:
python -m venv myenv
source myenv/bin/activate # On Windows, use `myenv\Scripts\activate`
pip install streamlit
To run a Streamlit app, create a Python file (e.g., app.py
) and add your Streamlit code. Then, run the following command in your terminal or command prompt:
streamlit run app.py
This will start the Streamlit server and open your app in a new browser window.
Here's a simple "Hello, World!" example to get you started with Streamlit:
import streamlit as st
st.title("Hello, World!")
st.write("This is a Streamlit app.")
When you run this code with streamlit run app.py
, you should see a web page with the title "Hello, World!" and the text "This is a Streamlit app."
Streamlit provides a variety of functions and widgets to create interactive web applications. Here are some of the most commonly used Streamlit features:
The st.write()
function is used to display text, data, and other objects in your Streamlit app. It can handle various data types, including strings, numbers, Pandas DataFrames, and more.
import streamlit as st
st.write("Hello, World!")
st.write(42)
st.write([1][2][3])
Streamlit supports Markdown formatting, allowing you to style your text with headings, lists, links, and more. You can use the st.markdown()
function to render Markdown content.
import streamlit as st
st.markdown("# This is a Heading")
st.markdown("- This is a bullet point")
st.markdown("[This is a link](https://streamlit.io/)")
You can add titles to your Streamlit app using the st.title()
function.
import streamlit as st
st.title("My Streamlit App")
Streamlit provides functions to add headers of different levels to your app.
import streamlit as st
st.header("This is a Header")
st.subheader("This is a Subheader")
import streamlit as st
st.text("This is some text.")
Streamlit allows you to display code snippets in your app using the st.code()
function.
import streamlit as st
code = """
def hello():
print("Hello, World!")
"""
st.code(code, language="python")
Streamlit supports rendering LaTeX equations using the st.latex()
function.
import streamlit as st
st.latex(r'''
a + ar + a r^2 + a r^3 + \cdots + a r^n =
\sum_{k=0}^n ar^k =
a \left(\frac{1-r^{n+1}}{1-r}\right)
''')
You can display JSON data in your Streamlit app using the st.json()
function.
import streamlit as st
data = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
st.json(data)
Streamlit provides a variety of interactive widgets that allow users to input data and control the behavior of your app.
The st.button()
function creates a button that can be clicked to trigger an action.
import streamlit as st
if st.button("Click me"):
st.write("Button clicked!")
The st.checkbox()
function creates a checkbox that can be used to toggle a boolean value.
import streamlit as st
agree = st.checkbox("I agree to the terms and conditions")
if agree:
st.write("Thank you for agreeing!")
The st.radio()
function creates a group of radio buttons, allowing the user to select one option from a list.
import streamlit as st
option = st.radio(
"Select an option",
("Option 1", "Option 2", "Option 3")
)
st.write(f"You selected: {option}")
The st.selectbox()
function creates a dropdown menu, allowing the user to select one option from a list.
import streamlit as st
option = st.selectbox(
"Select an option",
("Option 1", "Option 2", "Option 3")
)
st.write(f"You selected: {option}")
The st.multiselect()
function creates a multi-select widget, allowing the user to select multiple options from a list.
import streamlit as st
options = st.multiselect(
"Select options",
("Option 1", "Option 2", "Option 3")
)
st.write(f"You selected: {', '.join(options)}")
The st.slider()
function creates a slider widget, allowing the user to select a value within a specified range.
import streamlit as st
value = st.slider("Select a value", 0, 100, 50)
st.write(f"You selected: {value}")
The st.text_input()
function creates a text input field, allowing the user to enter text.
import streamlit as st
name = st.text_input("Enter your name", "Type here...")
st.write(f"Hello, {name}!")
The st.text_area()
function creates a multi-line text input area, allowing the user to enter longer text.
import streamlit as st
message = st.text_area("Enter a message")
st.write(f"You entered: {message}")
The st.date_input()
function creates a date input widget, allowing the user to select a date.
import streamlit as st
date = st.date_input("Select a date")
st.write(f"You selected: {date}")
The st.time_input()
function creates a time input widget, allowing the user to select a time.
import streamlit as st
time = st.time_input("Select a time")
st.write(f"You selected: {time}")
The st.file_uploader()
function creates a file uploader widget, allowing the user to upload files to your app.
import streamlit as st
file = st.file_uploader("Upload a file")
if file is not None:
st.write(f"File name: {file.name}")
st.write(f"File type: {file.type}")
st.write(f"File size: {file.size} bytes")
The st.color_picker()
function creates a color picker widget, allowing the user to select a color.
import streamlit as st
color = st.color_picker("Select a color")
st.write(f"You selected: {color}")
Streamlit provides several functions to display data in your app, including DataFrames, metrics, JSON, and tables.
The st.dataframe()
function displays a Pandas DataFrame in your Streamlit app.
import streamlit as st
import pandas as pd
data = {
"name": ["John", "Jane", "Bob"],
"age": [25, 30, 35]
}
df = pd.DataFrame(data)
st.dataframe(df)
The st.metric()
function displays a metric value with an optional label and optional delta value.
import streamlit as st
st.metric("Temperature", "25°C", "1.2°C")
The st.json()
function displays JSON data in your Streamlit app.
import streamlit as st
data = {
"name": "John Doe",
"age": 30,
"city": "New York"
}
st.json(data)
The st.table()
function displays a table in your Streamlit app.
import streamlit as st
import pandas as pd
data = {
"name": ["John", "Jane", "Bob"],
"age": [25, 30, 35]
}
df = pd.DataFrame(data)
st.table(df)
Streamlit supports various charting libraries, allowing you to create interactive visualizations in your app.
The st.line_chart()
function creates a line chart using Matplotlib or Altair.
import streamlit as st
import pandas as pd
import numpy as np
chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=["a", "b", "c"]
)
st.line_chart(chart_data)
The st.area_chart()
function creates an area chart using Matplotlib or Altair.
import streamlit as st
import pandas as pd
import numpy as np
chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=["a", "b", "c"]
)
st.area_chart(chart_data)
The st.bar_chart()
function creates a bar chart using Matplotlib or Altair.
import streamlit as st
import pandas as pd
import numpy as np
chart_data = pd.DataFrame(
np.random.randn(5, 3), columns=["a", "b", "c"]
)
st.bar_chart(chart_data)
The st.pie_chart()
function creates a pie chart using Matplotlib.
import streamlit as st
import pandas as pd
data = pd.DataFrame({
"category": ["A", "B", "C"],
"values": [30, 40, 30]
})
st.pie_chart(data["values"])
The st.altair_chart()
function creates an Altair chart in your Streamlit app.
import streamlit as st
import altair as alt
import pandas as pd
data = pd.DataFrame({
"a": [1, 2, 3, 4, 5],
"b": [1, 3, 2, 5, 4]
})
chart = alt.Chart(data).mark_line().encode(
x="a",
y="b"
)
st.altair_chart(chart, use_container_width=True)
The st.plotly_chart()
function creates a Plotly chart in your Streamlit app.
import streamlit as st
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
st.plotly_chart(fig)
The st.bokeh_chart()
function creates a Bokeh chart in your Streamlit app.
import streamlit as st
from bokeh.plotting import figure
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
p = figure(
title="Simple line example",
x_axis_label="x",
y_axis_label="y"
)
p.line(x, y, line_width=2)
st.bokeh_chart(p, use_container_width=True)
The st.deck_gl_chart()
function creates a Deck.gl chart in your Streamlit app.
import streamlit as st
import pandas as pd
import pydeck as pdk
data = pd.DataFrame({
"lat": [37.76, 37.77, 37.78],
"lon": [-122.4, -122.41, -122.42]
})
layer = pdk.Layer(
"HexagonLayer",
data=data,
get_position=["lon", "lat"],
auto_highlight=True,
elevation_scale=50,
pickable=True,
elevation_range=[0, 3000],
extruded=True,
)
st.deck_gl_chart(
viewport={
"latitude": 37.76,
"longitude": -122.4,
"zoom": 11,
"pitch": 50,
},
layers=[layer],
)
The st.graphviz_chart()
function creates a Graphviz chart in your Streamlit app.
import streamlit as st
import graphviz as graphviz
graph = graphviz.Digraph()
graph.edge("run", "intr")
graph.edge("intr", "kernel")
graph.edge("kernel", "zombie")
graph.edge("kernel", "sleep")
graph.edge("kernel", "runnable")
graph.edge("sleep", "runnable")
graph.edge("runnable", "run")
graph.edge("runnable", "kernel")
st.graphviz_chart(graph)
The st.pyplot()
function creates a Matplotlib chart in your Streamlit app.
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
arr = np.random.normal(1, 1, size=100)
fig, ax = plt.subplots()
ax.hist(arr, bins=20)
st.pyplot(fig)
Streamlit allows you to display various media types in your app, including images, audio, and video.
The st.image()
function displays an image in your Streamlit app.
import streamlit as st
st.image("https://example.com/image.jpg")
The st.audio()
function plays an audio file in your Streamlit app.
import streamlit as st
st.audio("https://example.com/audio.mp3")
The st.video()
function plays a video file in your Streamlit app.
import streamlit as st
st.video("https://example.com/video.mp4")
Streamlit provides several layout options to organize and structure your app's content.
The st.columns()
function creates multiple columns in your Streamlit app.
import streamlit as st
col1, col2 = st.columns(2)
with col1:
st.write("Column 1")
with col2:
st.write("Column 2")
The st.expander()
function creates an expandable section in your Streamlit app.
import streamlit as st
with st.expander("Click to expand"):
st.write("This content is hidden by default.")
The st.container()
function creates a container in your Streamlit app, allowing you to group elements together.
import streamlit as st
with st.container():
st.write("This content is inside a container.")
The st.sidebar
object allows you to create a sidebar in your Streamlit app.
import streamlit as st
with st.sidebar:
st.write("This content is in the sidebar.")
Streamlit provides state management capabilities to handle user interactions and maintain application state.
The st.session_state
object allows you to store and retrieve data across multiple sessions.
import streamlit as st
if "count" not in st.session_state:
st.session_state.count = 0
st.button("Increment", on_click=increment_count)
def increment_count():
st.session_state.count += 1
st.write(f"Count: {st.session_state.count}")
The st.state
object allows you to store and retrieve data within the same session.
import streamlit as st
if "count" not in st.state:
st.state.count = 0
st.button("Increment", on_click=increment_count)
def increment_count():
st.state.count += 1
st.write(f"Count: {st.state.count}")
Streamlit provides caching mechanisms to improve performance and reduce computation time.
The @st.memo
decorator caches the result of a function based on its input parameters.
import streamlit as st
@st.memo
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
n = st.number_input("Enter a number", value=10)
result = fibonacci(n)
st.write(f"The {n}th Fibonacci number is {result}")
The @st.cache_data
decorator caches the result of a function based on its input parameters and persists the cache across sessions.
import streamlit as st
import pandas as pd
@st.cache_data
def load_data(path):
return pd.read_csv(path)
path = st.text_input("Enter the file path")
data = load_data(path)
st.write(data)
The @st.cache_resource
decorator caches the result of a function and persists the cache across sessions, even if the function has no input parameters.
import streamlit as st
import
```python
import streamlit as st
import requests
@st.cache_resource
def load_data():
url = "https://example.com/data.csv"
return pd.read_csv(requests.get(url).content)
data = load_data()
st.write(data)
Streamlit allows you to customize the appearance of your app using themes and configuration settings.
The st.set_page_config()
function allows you to set various configuration options for your Streamlit app, such as the page title, layout, and icon.
import streamlit as st
st.set_page_config(
page_title="My App",
page_icon=":guardsman:",
layout="wide",
initial_sidebar_state="expanded"
)
Streamlit provides a built-in theming system that allows you to customize the appearance of your app using CSS.
import streamlit as st
# Set the theme
st.markdown(
"""
<style>
.stApp {
background-color: #F0F2F6;
color: #262730;
}
</style>
""",
unsafe_allow_html=True,
)
Streamlit apps can be deployed to various platforms, including Streamlit Sharing, Heroku, AWS, Azure, and GCP.
Streamlit Sharing is a free hosting service provided by Streamlit that allows you to share your app with others.
# Install the Streamlit Sharing CLI
pip install streamlit-sharing
# Deploy your app
streamlit-sharing deploy app.py
Streamlit Cloud is a paid hosting service provided by Streamlit that offers additional features and scalability.
# Install the Streamlit Cloud CLI
pip install streamlit-cloud
# Deploy your app
streamlit-cloud deploy app.py
You can deploy your Streamlit app to Heroku, a cloud platform for hosting web applications.
# Create a new Heroku app
heroku create my-streamlit-app
# Deploy your app
git push heroku master
You can deploy your Streamlit app to AWS using various services, such as Elastic Beanstalk, EC2, or ECS.
# Deploy your app to AWS Elastic Beanstalk
eb init
eb create my-streamlit-app
eb deploy
You can deploy your Streamlit app to Azure using various services, such as App Service or Azure Container Instances.
# Deploy your app to Azure App Service
az webapp up --name my-streamlit-app
You can deploy your Streamlit app to Google Cloud Platform (GCP) using various services, such as App Engine or Cloud Run.
# Deploy your app to GCP App Engine
gcloud app deploy
Streamlit supports custom components and community-contributed components, allowing you to extend the functionality of your app.
You can create custom Streamlit components using the Streamlit Components API.
import streamlit.components.v1 as components
# Create a custom component
my_component = components.declare_component("my_component", path="path/to/component")
# Use the custom component in your app
my_value = my_component(default=0)
st.write(f"Value: {my_value}")
Streamlit provides a repository of community-contributed components that you can install and use in your app.
# Install a community component
pip install streamlit-component-name
# Use the community component in your app
import streamlit_component_name
To ensure the best performance, maintainability, and user experience for your Streamlit app, it's essential to follow best practices.
Streamlit provides several techniques to optimize the performance of your app, such as caching, lazy evaluation, and state management.
import streamlit as st
# Use caching to avoid redundant computations
@st.cache_data
def expensive_computation(data):
# ...
return result
# Use lazy evaluation to avoid unnecessary computations
if st.checkbox("Run computation"):
result = expensive_computation(data)
st.write(result)
Streamlit provides various debugging tools and techniques to help you identify and fix issues in your app.
import streamlit as st
# Use the st.write() function to print debug information
st.write(f"Debug: {variable_value}")
# Use the st.stop() function to stop execution at a specific point
if condition:
st.stop()
Streamlit supports testing your app using various testing frameworks, such as pytest and unittest.
import streamlit as st
import pytest
def test_app():
# Create a test instance of the Streamlit app
st.start_test_mode()
# Run the app and perform assertions
assert st.button("Click me") == False
st.button("Click me")
assert st.button("Click me") == True
# Stop the test mode
st.stop_test_mode()
Streamlit provides various resources to help you learn and develop with the library, including documentation, tutorials, books, courses, and a community.
The official Streamlit documentation is a comprehensive resource that covers all aspects of the library, including installation, usage, and advanced topics.
Streamlit provides a collection of tutorials that cover various topics and use cases, helping you get started and learn by example.
Several books have been written on Streamlit, covering topics such as data visualization, machine learning, and web development.
- "Streamlit for Data Science" by Sharone Zitzman
- "Streamlit for Data Visualization" by Brock Mendel
Online courses are available to help you learn Streamlit and build applications for data science and machine learning.
- "Streamlit for Data Science" on Coursera
- "Streamlit for Data Visualization" on Udemy
Streamlit has an active community of developers and users who contribute to the library, share resources, and provide support.
Streamlit provides a variety of examples to help you get started and learn by example.
While Streamlit is a popular choice for building data science and machine learning applications, there are several alternatives available. Here are some of the most notable ones:
Dash is an open-source Python library for building analytical web applications, similar to Streamlit. It is built on top of Flask, React.js, and Plotly.js. Dash provides a more low-level and flexible approach compared to Streamlit, but it may require more web development knowledge.
Voila is a Python library that allows you to convert Jupyter Notebooks into interactive web applications. It provides a simple way to share and visualize data and models without writing additional code. Voila is a good choice if you primarily work with Jupyter Notebooks and want to share your work with others.
Panel is a Python library for building analytical web applications, similar to Streamlit and Dash. It is built on top of Bokeh, a library for creating interactive visualizations in the browser. Panel provides a more low-level and flexible approach compared to Streamlit, but it may require more web development knowledge.
Bokeh is a Python library for creating interactive visualizations in the browser. While Bokeh is primarily focused on data visualization, it can also be used to build web applications. Bokeh provides a more low-level and flexible approach compared to Streamlit, but it may require more web development knowledge.
Plotly Dash is a Python library for building analytical web applications, similar to Streamlit. It is built on top of Flask, React.js, and Plotly.js. Plotly Dash provides a more low-level and flexible approach compared to Streamlit, but it may require more web development knowledge.
Streamlit can be integrated with various Python libraries and frameworks to enhance its functionality and capabilities.
Streamlit integrates seamlessly with Pandas, a popular Python library for data manipulation and analysis. You can display Pandas DataFrames, perform data transformations, and create interactive visualizations using Streamlit's built-in functions.
import streamlit as st
import pandas as pd
data = pd.read_csv("data.csv")
st.dataframe(data)
Streamlit can be used in conjunction with NumPy, a fundamental Python library for scientific computing. You can perform numerical operations, create arrays, and visualize data using Streamlit's charting capabilities.
import streamlit as st
import numpy as np
arr = np.random.randn(100)
st.line_chart(arr)
Streamlit can be integrated with Scikit-Learn, a popular Python library for machine learning. You can build and deploy machine learning models, visualize model performance, and create interactive interfaces for model tuning and evaluation.
import streamlit as st
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
model = LogisticRegression()
model.fit(X, y)
st.write(f"Accuracy: {model.score(X, y):.2f}")
Streamlit can be used with TensorFlow, a popular open-source library for machine learning and deep learning. You can build and deploy TensorFlow models, visualize model performance, and create interactive interfaces for model tuning and evaluation.
import streamlit as st
import tensorflow as tf
# Load and preprocess data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Build and train the model
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test)
st.write(f"Accuracy: {accuracy:.2f}")
Streamlit can be integrated with PyTorch, a popular open-source machine learning library. You can build and deploy PyTorch models, visualize model performance, and create interactive interfaces for model tuning and evaluation.
import streamlit as st
import torch
import torchvision
import torchvision.transforms as transforms
# Load and preprocess data
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)
# Build and train the model
net = torchvision.models.resnet18(pretrained=False)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# get the inputs; data is a list of [inputs, labels]
inputs, labels = data
# forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
st.write(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')
running_loss = 0.0
st.write('Finished Training')
Streamlit can be integrated with Hugging Face, a popular open-source library for natural language processing (NLP). You can build and deploy NLP models, visualize model performance, and create interactive interfaces for text generation, sentiment analysis, and other NLP tasks.
import streamlit as st
from transformers import pipeline
# Load the sentiment analysis pipeline
sentiment_pipeline = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
# Get user input
text = st.text_area("Enter some text for sentiment analysis")
# Perform sentiment analysis
if text:
result = sentiment_pipeline(text)
st.write(f"Sentiment: {result['label']} (Score: {result['score']:.2f})")
Streamlit can be integrated with LangChain, a framework for building applications with large language models (LLMs). You can build and deploy LLM-powered applications, create interactive interfaces for querying LLMs, and leverage LangChain's capabilities for tasks like question answering, text generation, and summarization.
import streamlit as st
from langchain import PromptTemplate, OpenAI, LLMChain
# Set up the OpenAI LLM
llm = OpenAI(model_name="text-davinci-003")
# Define the prompt template
template = """
You are a helpful AI assistant. Given the following context, answer the question:
Context: {context}
Question: {question}
"""
prompt = PromptTemplate(template=template, input_variables=["context", "question"])
# Create the LLM chain
chain = LLMChain(prompt=prompt, llm=llm)
# Get user input
context = st.text_area("Enter the context")
question = st.text_input("Enter your question")
# Generate the answer
if context and question:
answer = chain.run(context=context, question=question)
st.write(f"Answer: {answer}")
Streamlit can be integrated with OpenAI's API, allowing you to build applications that leverage OpenAI's language models, such as GPT-3. You can create interactive interfaces for text generation, question answering, and other natural language processing tasks.
import streamlit as st
import openai
# Set up the OpenAI API key
openai.api_key = "YOUR_API_KEY"
# Get user input
prompt = st.text_area("Enter your prompt")
# Generate text using GPT-3
if prompt:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
generated_text = response.choices.text
st.write(f"Generated Text: {generated_text}")
Streamlit is an open-source project, and contributions from the community are welcome and encouraged. Here's how you can contribute to Streamlit:
The Streamlit project is hosted on GitHub, where you can find the source code, report issues, and submit pull requests.
If you encounter any bugs, issues, or have feature requests, you can report them on the Streamlit GitHub repository's issue tracker.
If you want to contribute code changes or new features to Streamlit, you can submit a pull request on the GitHub repository.
- Fork the Streamlit repository.
- Create a new branch for your changes.
- Make your changes and commit them with descriptive commit messages.
- Push your changes to your forked repository.
- Submit a pull request to the Streamlit repository, describing your changes and their purpose.
Streamlit has a Code of Conduct that outlines the expectations for behavior and interactions within the project community. All contributors are expected to follow this code of conduct.
Here are some frequently asked questions about Streamlit:
-
What is Streamlit? Streamlit is an open-source Python library that allows you to create interactive web applications for data science and machine learning projects with minimal effort.
-
How does Streamlit work? Streamlit uses a reactive programming model, where the app's user interface is automatically updated in response to user interactions or changes in the underlying data.
-
What are the advantages of using Streamlit? Streamlit offers several advantages, including ease of use, interactive widgets, data visualization capabilities, caching and state management, and easy deployment.
-
Can Streamlit be used for production applications? Yes, Streamlit can be used for production applications. It provides various deployment options, including Streamlit Sharing, Heroku, AWS, Azure, and GCP.
-
How does Streamlit handle state management? Streamlit provides built-in state management capabilities through the
st.session_state
andst.state
objects, allowing you to store and retrieve data across sessions or within the same session. -
Can Streamlit be integrated with other Python libraries? Yes, Streamlit can be integrated with various Python libraries, such as Pandas, NumPy, Scikit-Learn, TensorFlow, PyTorch, Hugging Face, and LangChain.
-
How can I contribute to Streamlit? You can contribute to Streamlit by reporting issues, submitting pull requests, or participating in the Streamlit community forums. Streamlit has a Code of Conduct that outlines the expectations for behavior and interactions within the project community.
-
What are some alternatives to Streamlit? Some alternatives to Streamlit include Dash, Voila, Panel, Bokeh, and Plotly Dash. Each library has its own strengths and weaknesses, and the choice depends on your specific requirements and preferences.
-
How can I learn more about Streamlit? You can learn more about Streamlit by exploring the official documentation, tutorials, books, courses, and community resources. Streamlit also provides a variety of examples to help you get started and learn by example.
-
Is Streamlit free to use? Yes, Streamlit is an open-source project and is free to use for both personal and commercial projects.
Here are some common terms and concepts used in the Streamlit ecosystem:
-
Streamlit App: A Python script that defines the user interface and functionality of a Streamlit application.
-
Streamlit Components: Reusable UI elements that can be used to build Streamlit applications, such as buttons, sliders, and charts.
-
Streamlit Caching: A mechanism provided by Streamlit to improve performance by caching the results of expensive computations or data transformations.
-
Streamlit Config: A set of configuration options that can be used to customize the appearance and behavior of a Streamlit application.
-
Streamlit Deployment: The process of making a Streamlit application available for others to use, either locally or on a remote server.
-
Streamlit Layout: The arrangement and organization of UI elements within a Streamlit application.
-
Streamlit Sharing: A free hosting service provided by Streamlit that allows you to share your Streamlit applications with others.
-
Streamlit State Management: The process of managing and persisting application state across user interactions and sessions.
-
Streamlit Theming: The process of customizing the appearance of a Streamlit application by modifying its CSS styles.
-
Streamlit Widgets: Interactive UI elements provided by Streamlit, such as buttons, sliders, and text inputs, that allow users to interact with the application.
Here's a concise cheat sheet for some of the most commonly used Streamlit functions and features:
st.write("Hello, World!") # Display text
st.write(42) # Display a number
st.write() # Display a list
st.markdown("# This is a Heading") # Render Markdown
st.button("Click me") # Button
st.checkbox("Check me") # Checkbox
st.radio("Select an option", ["Option 1", "Option 2"]) # Radio buttons
st.selectbox("Select an option", ["Option 1", "Option 2"]) # Dropdown
st.multiselect("Select options", ["Option 1", "Option 2"]) # Multi-select
st.slider("Select a value", 0, 100, 50) # Slider
st.text_input("Enter text") # Text input
st.text_area("Enter text") # Text area
st.date_input("Select a date") # Date input
st.time_input("Select a time") # Time input
st.file_uploader("Upload a file") # File uploader
st.color_picker("Select a color") # Color picker
st.dataframe(df) # Display a Pandas DataFrame
st.metric("Temperature", "25°C", "1.2°C") # Display a metric
st.json(data) # Display JSON data
st.table(df) # Display a table
st.line_chart(data) # Line chart
st.area_chart(data) # Area chart
st.bar_chart(data) # Bar chart
st.pie_chart(data) # Pie chart
st.altair_chart(chart) # Altair chart
st.plotly_chart(fig) # Plotly chart
st.bokeh_chart(plot) # Bokeh chart
st.deck_gl_chart(viewport, layers) # Deck.gl chart
st.graphviz_chart(graph) # Graphviz chart
st.pyplot(fig) # Matplotlib chart
col1, col2 = st.columns(2) # Create columns
with st.expander("Click to expand"): # Expander
st.write("This content is hidden by default.")
with st.container(): # Container
st.write("This content is inside a container.")
with st.sidebar: # Sidebar
st.write("This content is in the sidebar.")
if "count" not in st.session_state: # Session state
st.session_state.count = 0
st.button("Increment", on_click=increment_count)
if "count" not in st.state: # State
st.state.count = 0
st.button("Increment", on_click=increment_count)
@st.memo # Memoization
def fibonacci(n):
# ...
@st.cache_data # Cache data
def load_data(path):
# ...
@st.cache_resource # Cache resource
def load_data():
# ...
st.set_page_config(page_title="My App", page_icon=":guardsman:", layout="wide") # Config
st.markdown("""
<style>
.stApp {
background-color: #F0F2F6;
color: #262730;
}
</style>
""", unsafe_allow_html=True) # Themes
# Streamlit Sharing
pip install streamlit-sharing
streamlit-sharing deploy app.py
# Streamlit Cloud
pip install streamlit-cloud
streamlit-cloud deploy app.py
# Heroku
heroku create my-streamlit-app
git push heroku master
# AWS Elastic Beanstalk
eb init
eb create my-streamlit-app
eb deploy
# Azure App Service
az webapp up --name my-streamlit-app
# GCP App Engine
gcloud app deploy
Here are some tips and tricks to help you get the most out of Streamlit:
-
Use Caching: Streamlit provides caching mechanisms to improve performance and reduce computation time. Use
@st.memo
,@st.cache_data
, and@st.cache_resource
to cache expensive computations or data transformations. -
Leverage State Management: Streamlit's state management capabilities (
st.session_state
andst.state
) allow you to store and retrieve data across sessions or within the same session, enabling you to build stateful applications. -
Modularize Your Code: Break your Streamlit app into smaller, reusable components or functions to improve code organization and maintainability.
-
Use Streamlit Themes: Customize the appearance of your Streamlit app by using the built-in theming system or creating your own custom CSS styles.
-
Optimize Data Loading: If you're working with large datasets, consider loading and processing the data in a separate thread or process to avoid blocking the main Streamlit thread.
-
Leverage Streamlit Components: Streamlit provides a variety of built-in components and widgets, as well as a community-contributed component library, to enhance the functionality and user experience of your app.
-
Utilize Streamlit Layouts: Use Streamlit's layout features, such as columns, expanders, and containers, to organize and structure your app's content effectively.
-
Integrate with Other Libraries: Streamlit can be integrated with various Python libraries, such as Pandas, NumPy, Scikit-Learn, TensorFlow, PyTorch, Hugging Face, and LangChain, to enhance its capabilities.
-
Leverage Streamlit Deployment Options: Streamlit provides various deployment options, including Streamlit Sharing, Heroku, AWS, Azure, and GCP, allowing you to easily share and deploy your applications.
-
Participate in the Streamlit Community: Join the Streamlit community forums, contribute to the project on GitHub, and stay up-to-date with the latest developments and best practices.
Streamlit is an actively developed and maintained project, with a roadmap that outlines the future plans and goals for the library. Here are some of the key areas and features that are part of the Streamlit roadmap:
-
Performance Improvements: Streamlit aims to continuously improve the performance and responsiveness of the library, particularly for large-scale applications and datasets.
-
Enhanced Data Visualization: Streamlit plans to expand its data visualization capabilities by integrating with more charting libraries and providing advanced visualization features, such as interactive dashboards and real-time updates.
-
Improved Theming and Customization: Streamlit aims to provide more flexible and powerful theming options, allowing users to customize the appearance and branding of their applications more easily.
-
Streamlined Deployment: Streamlit aims to simplify the deployment process further, making it easier to deploy applications to various platforms and environments.
-
Expanded Component Ecosystem: Streamlit plans to foster a growing ecosystem of community-contributed components, enabling users to extend the functionality of their applications with reusable and modular components.
-
Improved Developer Experience: Streamlit aims to enhance the developer experience by providing better tooling
-
, improved debugging capabilities, and more comprehensive documentation and tutorials.
-
Collaboration and Sharing: Streamlit plans to introduce features that facilitate collaboration and sharing of applications, enabling teams to work together more effectively on data science and machine learning projects.
-
Improved Accessibility: Streamlit aims to enhance the accessibility of its applications, ensuring that they are usable by individuals with disabilities and comply with accessibility standards.
-
Expanded Language Support: Streamlit plans to explore support for additional programming languages beyond Python, allowing developers from different backgrounds to leverage the power of Streamlit.
-
Integration with Cloud Services: Streamlit aims to provide seamless integration with various cloud services, enabling users to leverage cloud resources for data storage, computation, and deployment.
-
Enhanced Security and Privacy: Streamlit plans to implement additional security measures and privacy features to ensure the protection of sensitive data and compliance with relevant regulations.
-
Improved Scalability: Streamlit aims to enhance the scalability of its applications, enabling them to handle large-scale data and high-concurrency workloads more efficiently.
-
Expanded Use Cases: Streamlit plans to explore new use cases and domains beyond data science and machine learning, such as business intelligence, financial analytics, and scientific computing.
-
Community Engagement: Streamlit aims to foster a strong and engaged community by encouraging contributions, providing support channels, and organizing events and meetups.
The Streamlit roadmap is subject to change based on user feedback, technological advancements, and the evolving needs of the data science and machine learning communities. Streamlit's development team actively collaborates with the community to prioritize and implement new features and improvements.
Streamlit maintains a detailed changelog that documents the changes, bug fixes, and new features introduced in each release. Here's a summary of some of the notable changes in recent Streamlit releases:
- Added support for Streamlit Components in Streamlit Cloud and Streamlit Sharing.
- Improved performance and memory usage for large Pandas DataFrames.
- Introduced
st.metric()
for displaying metrics with optional delta values. - Added support for custom CSS in Streamlit Cloud and Streamlit Sharing.
- Improved error handling and debugging capabilities.
- Introduced
st.cache_resource()
for caching resources across sessions. - Added support for Streamlit Components in Streamlit Cloud.
- Improved performance and memory usage for large datasets.
- Introduced
st.set_page_config()
for configuring page settings. - Added support for custom themes in Streamlit Cloud and Streamlit Sharing.
- Introduced
st.state
for managing state within a single session. - Added support for Streamlit Components in Streamlit Sharing.
- Improved performance and memory usage for large datasets.
- Introduced
st.deck_gl_chart()
for rendering Deck.gl visualizations. - Added support for custom CSS in Streamlit Sharing.
- Introduced
st.cache_data()
for caching data across sessions. - Added support for Streamlit Components in local development.
- Improved performance and memory usage for large datasets.
- Introduced
st.graphviz_chart()
for rendering Graphviz visualizations. - Added support for custom CSS in local development.
- Introduced
st.memo
for memoizing expensive computations. - Added support for Streamlit Components in Streamlit Cloud.
- Improved performance and memory usage for large datasets.
- Introduced
st.altair_chart()
for rendering Altair visualizations. - Added support for custom themes in Streamlit Cloud.
For a complete list of changes and detailed release notes, please refer to the official Streamlit changelog: Streamlit Changelog
Streamlit is released under the Apache License 2.0, which is a permissive open-source license that allows for commercial and non-commercial use, modification, and distribution of the software.
The Apache License 2.0 grants the following permissions:
- Commercial Use: You can use Streamlit for commercial purposes, including in proprietary software.
- Modification: You can modify the Streamlit source code and create derivative works.
- Distribution: You can distribute copies of Streamlit, either in its original form or as part of your own software.
- Patent Grant: The license includes a patent grant, which means that contributors to the project grant a patent license for their contributions.
The Apache License 2.0 also includes the following requirements:
- License and Copyright Notice: You must include the license and copyright notice in any copies or derivative works of Streamlit.
- State Changes: If you modify the Streamlit source code, you must clearly indicate that changes were made.
- Disclaimer of Warranty: The software is provided "as is," without warranty of any kind, express or implied.
- Limitation of Liability: The contributors of Streamlit are not liable for any damages arising from the use of the software.
By using Streamlit, you agree to comply with the terms and conditions of the Apache License 2.0. For more information, please refer to the official Apache License 2.0 documentation: Apache License 2.0
Streamlit is an open-source project, and contributions from the community are welcome and encouraged. If you're interested in contributing to Streamlit, here are some guidelines to follow:
-
Fork the Repository: Start by forking the Streamlit repository on GitHub. This will create a copy of the repository in your GitHub account.
-
Create a Branch: Create a new branch in your forked repository for your changes. It's a good practice to use a descriptive branch name that reflects the purpose of your changes.
-
Make Changes: Implement your changes or new features in your branch. Ensure that your code follows the Streamlit coding style and conventions.
-
Test Your Changes: Before submitting your changes, make sure to test them thoroughly. Streamlit provides testing utilities and frameworks to help you write and run tests.
-
Commit Your Changes: Commit your changes with clear and descriptive commit messages. This will help other contributors understand the purpose and context of your changes.
-
Push to Your Fork: Push your branch with the committed changes to your forked repository on GitHub.
-
Submit a Pull Request: Navigate to the original Streamlit repository on GitHub and submit a pull request (PR) from your forked branch. Provide a detailed description of your changes, including any relevant information or context.
-
Respond to Feedback: The Streamlit maintainers and community members may provide feedback or request changes to your PR. Be responsive and address any concerns or suggestions raised.
-
Merge Your Changes: Once your PR has been reviewed and approved, it will be merged into the Streamlit codebase by the maintainers.
Before contributing, make sure to review the Streamlit Code of Conduct and follow the guidelines outlined in the project's contributing documentation. Additionally, it's recommended to familiarize yourself with the Streamlit development practices, coding style, and testing frameworks.
By contributing to Streamlit, you'll be part of a vibrant community dedicated to improving and expanding the capabilities of this powerful data science and machine learning library.
The Streamlit project has adopted a Code of Conduct to ensure a respectful, inclusive, and harassment-free environment for all contributors and community members. The Code of Conduct outlines the expected behavior and guidelines for participation in the Streamlit project.
Here are some key points from the Streamlit Code of Conduct:
-
Be Respectful: Treat all individuals with respect, regardless of their background, identity, or experience. Harassment, discrimination, and offensive behavior will not be tolerated.
-
Be Inclusive: Embrace diversity and create an inclusive environment where everyone feels welcome and valued. Respect different perspectives and experiences.
-
Be Professional: Maintain a professional and constructive attitude in all interactions and communications related to the project. Avoid personal attacks, insults, or derogatory language.
-
Be Collaborative: Collaborate openly and constructively with other community members. Provide and accept feedback graciously, and work together towards the project's goals.
-
Be Responsible: Take responsibility for your actions and words. If you witness or experience any violation of the Code of Conduct, report it to the project maintainers.
-
Be Ethical: Uphold ethical standards and act with integrity. Respect intellectual property rights, privacy, and confidentiality.
-
Be Mindful: Be mindful of your words and actions, and consider how they may impact others. Avoid assumptions or biases, and strive to create a welcoming environment for all.
The Streamlit Code of Conduct applies to all project spaces, including the codebase, issue tracker, pull requests, community forums, and any other project-related communication channels. Violations of the Code of Conduct may result in temporary or permanent consequences, as determined by the project maintainers.
By participating in the Streamlit project, you agree to abide by the Code of Conduct and contribute to fostering a positive and inclusive community. For more details, please refer to the official Streamlit Code of Conduct: Streamlit Code of Conduct
Streamlit takes security seriously and follows best practices to ensure the safety and integrity of its applications and the data they handle. Here are some key security considerations and measures implemented by Streamlit:
-
Input Validation: Streamlit performs input validation to prevent potential security vulnerabilities, such as cross-site scripting (XSS) and code injection attacks. User input is sanitized and validated before being processed or displayed.
-
Secure Deployment: Streamlit provides secure deployment options, including Streamlit Sharing and Streamlit Cloud, which are hosted on secure infrastructure and follow industry-standard security practices.
-
Secure Communication: When deploying Streamlit applications, communication between the client and server is encrypted using HTTPS to protect data in transit.
-
Dependency Management: Streamlit relies on well-maintained and secure third-party dependencies, which are regularly updated to address potential vulnerabilities.
-
Secure Development Practices: The Streamlit development team follows secure coding practices, including code reviews, static code analysis, and regular security audits.
-
Responsible Disclosure: Streamlit has a responsible disclosure policy in place, encouraging security researchers and users to report any potential vulnerabilities or security issues they discover.
-
Regular Updates and Patches: Streamlit releases regular updates and security patches to address any identified vulnerabilities or security concerns.
-
Security Documentation: Streamlit provides documentation and guidelines on secure deployment, configuration, and usage of its applications, helping users implement security best practices.
-
Community Engagement: Streamlit actively engages with the security community and encourages responsible disclosure of potential vulnerabilities, fostering a collaborative approach to security.
While no software is completely immune to security risks, Streamlit is committed to maintaining a high level of security and continuously improving its security practices. Users are encouraged to follow best practices, keep their Streamlit installations up-to-date, and report any potential security issues through the appropriate channels.
For more information on Streamlit's security practices and responsible disclosure policy, please refer to the official Streamlit documentation and community resources.
Streamlit values feedback from its users and community members. Your feedback helps the Streamlit team understand your needs, identify areas for improvement, and prioritize future development efforts.
Here are some ways you can provide feedback to the Streamlit team:
-
GitHub Issues: If you encounter any bugs, issues, or have feature requests, you can open a new issue on the Streamlit GitHub repository: Streamlit GitHub Issues
-
Community Forum: Streamlit has an active community forum where you can share your feedback, ask questions, and engage with other users and developers: Streamlit Community Forum
-
Twitter: You can share your feedback, experiences, or suggestions with the Streamlit team and community on Twitter: Streamlit on Twitter
-
Email: If you prefer to provide feedback directly to the Streamlit team, you can send an email to [email protected]
-
Surveys and Feedback Forms: Streamlit occasionally conducts surveys or shares feedback forms to gather input from users on specific topics or features.
When providing feedback, it's helpful to include the following information:
- A clear and concise description of your feedback, issue, or feature request.
- Steps to reproduce the issue (if applicable).
- Any relevant error messages or logs.
- Your use case or context for the feedback.
- Screenshots or examples (if applicable).
- Your Streamlit version and environment details.
The Streamlit team values all feedback and takes it into consideration during the development and planning process. Your feedback helps shape the future of Streamlit and ensures that it continues to meet the needs of the data science and machine learning communities.
Thank you for your contributions and for being part of the Streamlit community!