StateSession

Hi guys ,

I have seen this code in a post

import streamlit as st
import pandas as pd
import numpy as np
import SessionState 

# https://gist.githubusercontent.com/tvst/036da038ab3e999a64497f42de966a92/raw/f0db274dd4d295ee173b4d52939be5ad55ae058d/SessionState.py

# Create an empty dataframe
data = pd.DataFrame(columns=["Random"])
st.text("Original dataframe")

# with every interaction, the script runs from top to bottom
# resulting in the empty dataframe
st.dataframe(data) 

# persist state of dataframe
session_state = SessionState.get(df=data)

# random value to append; could be a num_input widget if you want
random_value = np.random.randn()

if st.button("Append random value"):
    # update dataframe state
    session_state.df = session_state.df.append({'Random': random_value}, ignore_index=True)
    st.text("Updated dataframe")
    st.dataframe(session_state.df)

# still empty as state is not persisted
st.text("Original dataframe")
st.dataframe(data)

I would like to know if I have had multiple columns apart “Random” , how would I have dealed with this line

session_state.df = session_state.df.append({'Random': random_value}, ignore_index=True)

taking into account all columns : example “col1” and “col2”

Thanks for your help

Problems, questions, code snippets are usually discussed in the streamlit category “Using streamlit”.

“Show the community” is meant to show case finished projects you did.

Since that prototype of state management was devised, Streamlit has released an official state management API:

I would suggest working through those examples, then seeing if you’re still having issues.

Best,
Randy

thanks for the answer .

This is what I have found yesterday and I came up with this from a snippet code of someone

session_state.df = session_state.df.append({“coin” : coin}, ignore_index=True)

However , I want to use this technique with multiple columns …

How to do it ?

Thx

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