Is there anyway to append a new value to a single column dataframe each time a button is clicked? I have tried to implement it using st.cache but the cached values are those every time the script reruns and not every time the button is pressed. The dataframe is empty initially.
Hi @Yashvin_Jagarlamudi . Could you please make available a snipp of you code and a screenshot of the problem you facing, i might be able to help you. Looking over the top of your question, i think itās a logic problem and nothing correlation with streamlit problem.
Hi @Yashvin_Jagarlamudi, welcome to the Streamlit community!!
Your question sort of sounds like maintaining session state. Maybe take a look at this post or our session-state tag to see if it helps with this problem.
Hereās a simple working example that shows how to grow a dataframe a single row at a time using SessionState and clicking a button:
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)
Happy Streamlit-ing!
Snehan
@feliperoque I was trying to do something similar to what @snehankekre has below
thank you for help. This is what I was looking for but I couldnāt figure out how to do it in with Session State!
I wanted to know if you had multiple columns apart "Random
How to do the
āsession_state.df = session_state.df.append({āRandomā: random_value}, ignore_index=True)
st.text(āUpdated dataframeā)ā
I didnāt find a solution even if it seems obvious ?.
THnaks
This code throws error @snehankekre
This is an adaptation I believe works as intended on 1.7.0 (new session_state):
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)
if 'df' not in st.session_state:
st.session_state.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
st.session_state.df = st.session_state.df.append({'Random': random_value}, ignore_index=True)
st.text("Updated dataframe")
st.dataframe(st.session_state.df)
# still empty as state is not persisted
st.text("Original dataframe")
st.dataframe(data)
Dataframe add row action and download.
import pandas as pd
import streamlit as st
# "st.session_state object:", st.session_state
if "df_result" not in st.session_state:
st.session_state['df_result'] = pd.DataFrame(columns=['h1','h2'])
# st.write(st.session_state)
def onAddRow():
data = {
'h1':"something",
'h2':"something",
}
st.session_state['df_result'] = st.session_state['df_result'].append(data, ignore_index=True)
st.button("Add row", on_click = onAddRow)
@st.cache
def convert_df(df):
return df.to_csv().encode('utf-8')
st.download_button(
"Press to Download",
convert_df(st.session_state.df_result),
"file.csv",
"text/csv",
key='download-csv'
)
st.dataframe(st.session_state['df_result'])
I want to make like this but in this tutorial we can make a new row with pandas and numpy. I have problem with pandas to make table or dataframe so i use jinja2 to make table or dataframe. Can someone help me to make the code so can work with streamlit and jinja2 ?
before i asking in this section, i try to with [Natakran_Pimthong] code and [gerardrbentley] code. But i have problem when i append the item.
So the code cannot add new jinja2 dataframe. only can make in 1 dataframe. so i stuck. please help me
Hi, thanks for this solution.
However, as I am taking input from the web appās interface, and i have to input the values of those h1 and h2 myself, it starts with 0 and on key press it inputs those initial value into the dataframe, and when I change the input of the dataframe, the one before it gets saved again and then it saves the new one in the dataframe
i just want to be able to add my input on key press, directly into the dataframe
Thanks