Can you provide an example of a basic Streamlit application that generates a simple chart?

41 viewsStreamlit
0

Can you provide an example of a basic Streamlit application that generates a simple chart from a dataset?

Daniel Steinhold Asked question September 17, 2023
0

Sure. Here is an example of a basic Streamlit application that generates a simple bar chart from a dataset:

Python
import streamlit as st
import pandas as pd

# Load the dataset
df = pd.read_csv('dataset.csv')

# Select the data for the chart
x = df['column_name_1']
y = df['column_name_2']

# Create the bar chart
st.bar_chart(df, x, y)

To run this application, save it as a Python file (e.g. app.py) and then run the following command in a terminal:

streamlit run app.py

This will open a web browser with the Streamlit application. You should see a bar chart with the data from the dataset.

You can customize the bar chart by changing the parameters of the st.bar_chart() function. For example, you can change the title of the chart, the labels for the x and y axes, and the colors of the bars.

Here is an example of a customized bar chart:

Python
import streamlit as st
import pandas as pd

# Load the dataset
df = pd.read_csv('dataset.csv')

# Select the data for the chart
x = df['column_name_1']
y = df['column_name_2']

# Create the bar chart
st.bar_chart(
    df,
    x,
    y,
    title='My Bar Chart',
    xlabel='X Axis',
    ylabel='Y Axis',
    colors=['red', 'green', 'blue']
)

This will create a bar chart with the title My Bar Chart, the x-axis label X Axis, the y-axis label Y Axis, and the bars colored red, green, and blue.

You can also add interactive widgets to your Streamlit applications. For example, you could add a drop-down menu that allows users to select different data columns to visualize in the chart.

Here is an example of a Streamlit application with a drop-down menu:

Python
import streamlit as st
import pandas as pd

# Load the dataset
df = pd.read_csv('dataset.csv')

# Create a drop-down menu
column_name = st.selectbox('Select a column to visualize:', df.columns)

# Select the data for the chart
x = df[column_name]

# Create the bar chart
st.bar_chart(x)

This will create a Streamlit application with a drop-down menu that allows users to select different data columns to visualize in the chart. When the user selects a column, the chart will be updated to show the data for that column.

Streamlit is a powerful and easy-to-use tool for creating interactive data applications. You can use it to create a wide variety of applications, from simple data visualizations to complex dashboards to machine learning models.

Daniel Steinhold Changed status to publish September 17, 2023