Why make interactive visualizations?
- Visualization is an important part of both exploratory data analysis and communicating results
- Interactivity allows us to visualize more information without overcomplicating a single plot
Create a New Environment
- use
conda env create --file environment.ymlto create a new environment from a YAML file - see a list of all environments with
conda env list - activate the new environment with
conda activate <NAME> - see a list of all installed packages with
conda list
Data Wrangling
- Import your CSV using
pd.read_csv('<FILEPATH>') - Transform your dataframe from wide to long with
pd.melt() - Split column values with
df['<COLUMN>'].str.split('<DELIM>') - Sort rows using
df.sort_values() - Export your dataframe to CSV using
df.to_csv('<FILEPATH>')
Create Visualizations
- Before visualizing your dataframe, make sure it only includes the
rows you want to visualize. You can use pandas’
query()function to easily accomplish this - To make a line plot with
px.line, you need to specify the dataframe, X axis, and Y axis - If you want to have multiple lines, you also need to specify what column determines the line color
- In a Jupyter Notebook, you need to call
fig.show()to display the chart
Create the Streamlit App
- The entire streamlit app must be saved in a single python file,
typically
app.py - To run the app locally, enter the bash command
streamlit run app.py - Add a title with
st.title('Title'), and other text withst.write('## Markdown can go here') - Make sure your dataframes and figures are stored in variables,
typically
dffor a dataframe andfigfor a figure - To display a plotly figure, use
st.plotly_chart(fig)
Refactoring Code for Flexibility (Prepping for Widgets)
- In order to add widgets, we need to refactor our code to make it more flexible.
- f-Strings allow you to easily insert variables into a string
Add Widgets to the Streamlit App
- Add a widget to select the continent or metric from a list with
st.selectbox() - There are two ways to add a widget to the sidebar:
st.sidebar.<widget>()andwith st.sidebar: st.<widget>()
Publish Your Streamlit App
- All Streamlit apps must have a GitHub repo with the code, data, and environment files
- You can deploy up to 3 apps for free with Streamlit Cloud