Hello, I would like to show a data frame block by block using the st.session_state function, I found a solution here (Navigating through dataframe records - #2 by AvratanuBiswas). However, my situation is a bit different in that I need to query to download the data. After following the code in the link above, every time when I click next button, it will run through the whole code to re-download the whole dataset and go to the next row. Is there a way to avoid re-downloading the data? Thank you. @AvratanuBiswas
A simplified version of the code looks like the following:
import streamlit as st
import pandas as pd
import trino
def get_data_and_show():
conn = trino.dbapi.connect(
host='...',
port=443,
user="...",
auth='...',
http_scheme='https',
catalog="..."
)
sql = '''my query'''
cursor = conn.cursor()
cursor.execute(sql)
df = pd.DataFrame(cursor.fetchall(), columns=[i[0] for i in cursor.description])
ids = df["id"].unique()
n = len(ids)
if "page" not in st.session_state:
st.session_state.page = 0
def next_seqid():
st.session_state.page += 1
def prev_seqid():
st.session_state.page -= 1
if st.session_state.page < n-1:
st.button("Next", on_click=next_seqid)
if st.session_state.page > 0:
st.button("Previous", on_click=prev_seqid)
id = ids[st.session_state.page]
df_tmp = df[df["id"]==id]
st.dataframe(df_tmp)
get_data_and_show()