import streamlit as st
import pandas as pd
import plotly.express as px
= 'https://files.catbox.moe/eor4ta.csv'
url = pd.read_csv(url).melt(id_vars=["Name", "Gender"]).dropna().rename(
name_lookup_df ={"variable": "Year", "value": "Rank"}
columns
)
= pd.DataFrame(name_lookup_df['Name'].value_counts())
names_appearing_most_years
st.dataframe(names_appearing_most_years)
18 Downloading tabular data files (e.g. pandas dataframes)
Many outputs from Streamlit apps benefit from being able to be downloaded after they have been edited or calculated.
For example, your users may want to be able to download the data that is behind a graph, or a summary csv of the data they have uploaded.
They may also want to save the graphs or other image outputs they create via your apps, like wordclouds, to their computer.
In this chapter, we’re going to focus on tabular data, as you would display in an Excel file or pandas dataframe.
18.1 Saving pandas dataframes
For now, we’ll assume the tabular data you want to save is in a pandas dataframe.
This will usually be the case - or it will be data you can easily transform into a pandas dataframe, like a numpy array.
Let’s start with an app that has some data on the popularity of names.
This app currently just loads in a dataset, does some simple manipulations, then returns the names that appeared most frequently in the dataset.
18.1.1 Saving as csv or excel file
If a dataframe is displayed using the standard st.dataframe()
command or the st.write()
command, a csv download option will become visible when hovering over the dataframe.