on user action (be it a click or, preferably, a selection) of a row/cell
i would like to show content of a json file to st.json below the table.
import streamlit as st
import pandas as pd
# sample DataFrame
data = {
"Column 1": [1, 2],
"Column 2": ['A', 'B']
}
#sample json
jdata ='''
{
"glossary": {
"title": "example glossary",
}
}
'''
# Create a DataFrame from the sample data
df = pd.DataFrame(data)
# Display the DataFrame in the Streamlit app
st.dataframe(df,
column_config={
"json": st.column_config.LinkColumn(
"JSON",
width='small',
)
}
)
#only show the following on click of row/cell
st.json(jdata)
If youâd like to use the st.data_editor, you can replace st.dataframe with st.data_editor.
In order to trigger a function upon widget interaction or changes to the dataframe, you can make use of the on_change parameter by assigning it a callback function that essentially will tell it what to do once the trigger occurs.
thank you for the links, mybe iâll rephrase my question as i dont see the solution in those links. |
how do i retrun on callback the specific cell/row i clicked?
Hello, I have a somewhat similar question.
In my dataframe, say I have a Pay_Due, Pay_Amount, and Pay_Balance columns, among others. How can I make the Pay_Balance be automatically computed as Pay_Amount - Pay_Due inside st.data_editor(), so when I make an update to any Pay_Amount value in my dataframe, it gets picked up by the component and updates it automatically?
import streamlit as st
from streamlit import session_state as ss
import pandas as pd
data = {
'Name': ['AAA', 'BBB', 'CCC'],
'Pay Due': [200, 200, 200],
'Pay Amount': [50, 50, 50],
'Pay Balance': [0, 0, 0]
}
if 'df' not in ss:
ss.df = pd.DataFrame(columns=list(data.keys()))
def change():
# Get the edited row index, column name and value.
er = ss.pay['edited_rows']
affected_index = list(er.keys())[0]
affected_pay_amount = er[affected_index]['Pay Amount']
# Update the pay amount at the affected index.
ss.df.at[affected_index, 'Pay Amount'] = affected_pay_amount
# Calculate the new value for pay balance at the affected index.
pay_bal = ss.df.at[affected_index, 'Pay Amount'] - ss.df.at[affected_index, 'Pay Due']
# Update the value of pay_bal at the affected index.
ss.df.at[affected_index, 'Pay Balance'] = pay_bal
def main():
if not len(ss.df):
ss.df = pd.DataFrame(data)
st.data_editor(ss.df, hide_index=True, on_change=change, key='pay')
if __name__ == '__main__':
main()
@ferdy
Since you are a Streamlit moderator, I wanted to ask this question that, because after many research on Streamlit forum and looking up the Streamlit Deployment guides posted on đ Deployment - Streamlit, I was not able to get an answer.
How can I deploy my Streamlit app on a shared hosting site? My site is operated by A2hosting.com and I have a domain I own that I would like to have my Streamlit app be myapp.mydomain.com.
A2hosting allow python apps via either Flask or Django frameworks. I followed their guide but both frameworks differ from Streamlit, and I much prefer Streamlit and donât want to rewrite my apps. Their customer service was not able to provide me with a new Guide specifically to run Streamlit on their site (could be many reasons).
Is there a way I can get answers to this question? How to deply my Streamlit apps on my own hosted domain?
Thanks in advance.
Tony
I used the same code but in my st.session_state.edited_rows there are more than one values. Its recording the history edits too in the same session.
Any idea how to resolve that, I am getting an output like this:
{âedited_rowsâ: {3: {â2024-02-25â: 2024, â2024-02-04â: 21138, â2024-02-11â: 209}, 7: {â2024-02-11â: 9}}, âadded_rowsâ: , âdeleted_rowsâ: }
Note: Deleting the state like this is not working as expected:
if âeditorâ in st.session_state:
del st.session_state.editor[âedited_rowsâ]
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking âAccept allâ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.