How to download .csv with a hyperlink/cell inside a dataframe

Hi community,

Is it to download a .csv using a hyperlink other than st.download_button ? Something like this :


And how can we suppose the URL of .csv ? For example, when I download a .csv from my app (hosted locally), the .csv has a url like this : http://localhost:8501/media/2a7651a0659c83163aea6d6321af3f86945b9ad133abd92c7eebeed2.csv?title=CarsApp

For more details, I posted a question in SO.

Thank you for your help !

I think the suggestion on SO is a good one, to use st.download_button python - How to add a hyperlink for downloading a dataframe as csv using Pandas - Stack Overflow. I added a comment about using pagination if the resulting table is too large to generate quickly.

1 Like

Hi @blackary, thank you so much.
I tried to use pagination but I got an erro in line 11

session_state = st.session_state.get(page_number=0)

TypeError: get() got an unexpected keyword argument โ€˜page_numberโ€™

Do you know how can we fix this, please ?

@nou7lh st.session_state has a very similar API to a normal python dictionary, so to get a specific entry you can either do:
st.session_state["page_number"] (note that this will raise a KeyError if there is no page_number)

or st.session_state.get("page_number", 0) (this will either return the page_number, if itโ€™s been set, or return 0, the default.

Hereโ€™s a related example: How to create streamlit app with pagination - #2 by ditw11mhs

1 Like

I tried with st.session_state['page_number'] and it worked !

Here is the code Iโ€™m using :

import pandas as pd
import streamlit as st

# Number of entries per screen
N = 15

st.markdown("# Demonstrating use of Next button with Session State")

# A variable to keep track of which product we are currently displaying

if 'page_number' not in st.session_state:
    st.session_state['page_number'] = 0

data = pd.read_csv("csv\cars.csv", delimiter=';', skiprows=[1])
last_page = len(data) // N

# Add a next button and a previous button

prev, _, next = st.columns([1, 10, 1])

if next.button("Next"):

    if st.session_state['page_number'] + 1 > last_page:
        st.session_state['page_number'] = 0
        st.session_state['page_number'] += 1

if prev.button("Previous"):

    if st.session_state['page_number'] - 1 < 0:
        st.session_state['page_number'] = last_page
        st.session_state['page_number'] -= 1

# Get start and end indices of the next page of the dataframe
start_idx = st.session_state['page_number'] * N
end_idx = (1 + st.session_state['page_number']) * N

# Index into the sub dataframe
sub_df = data.iloc[start_idx:end_idx]

With st.session_state.get("page_number", 0) instead, I donโ€™t know why I kept getting errors like this one :

start_idx = session_state['page_number'] * N

TypeError: โ€˜intโ€™ object is not subscriptable

Thank you so much Zackary.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.