# StateSession

**URL:** <https://discuss.streamlit.io/t/statesession/19227>\
**Category:** Using Streamlit\
**Created:** [November 18, 2021, 6:00am UTC](https://discuss.streamlit.io/t/statesession/19227 "2021-11-18T06:00:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iheb](https://avatars.discourse-cdn.com/v4/letter/i/7bcc69/32.png) [@Iheb](https://discuss.streamlit.io/u/Iheb)\
**Post date:** [November 18, 2021, 6:00am UTC](https://discuss.streamlit.io/t/statesession/19227/1 "2021-11-18T06:00:05Z")

</div>

Hi guys ,

I have seen this code in a post

```auto
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

---

<div class="post-metadata">

**Author:** ![MaxMnemo](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/maxmnemo/32/6093_2.png) [@MaxMnemo](https://discuss.streamlit.io/u/MaxMnemo)\
**Post date:** [November 18, 2021, 9:18am UTC](https://discuss.streamlit.io/t/statesession/19227/2 "2021-11-18T09:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/randyzwitch/32/58772_2.png) [@randyzwitch](https://discuss.streamlit.io/u/randyzwitch)\
**Post date:** [November 18, 2021, 2:13pm UTC](https://discuss.streamlit.io/t/statesession/19227/3 "2021-11-18T14:13:11Z")

</div>

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

> **[Add statefulness to apps - Streamlit Docs](https://docs.streamlit.io/library/advanced-features/session-state)**

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

Best,  
Randy

---

<div class="post-metadata">

**Author:** ![Iheb](https://avatars.discourse-cdn.com/v4/letter/i/7bcc69/32.png) [@Iheb](https://discuss.streamlit.io/u/Iheb)\
**Post date:** [November 18, 2021, 2:17pm UTC](https://discuss.streamlit.io/t/statesession/19227/4 "2021-11-18T14:17:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [November 18, 2022, 2:18pm UTC](https://discuss.streamlit.io/t/statesession/19227/5 "2022-11-18T14:18:29Z")

</div>

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