I am working locally with streamlit. I’m using strealmit to query my search engine and get the result back. I send a query to the search engine and convert the result set into a dataframe and write this dataframe
When I run the streamlit, the above UI showed up. The first column is a hyperlink (ID) which I would like to click and call a Python script that takes that ID, calls a rest API for that ID, and return some result back and show that result on the page.
Here’s a slightly different approach, that adds a boolean column and a dataframe editor which lets you click on a row and get the id from that, and then resets the editor so that you can only select at most one row at a time:
import streamlit as st
import pandas as pd
PATENT_DATA = {
"patent_id": ["US-123", "US-456", "US-789", "US-101"],
"description": [
"Some description",
"Some description",
"Some description",
"Some description",
],
}
ID_COL = "patent_id"
if "data" not in st.session_state:
st.session_state["data"] = pd.DataFrame(
{
"select": [False, False, False, False],
**PATENT_DATA,
}
)
if "editor_id" not in st.session_state:
st.session_state["editor_id"] = 0
if "last_selected" not in st.session_state:
st.session_state["last_selected"] = None
def edited():
editor_state = st.session_state[st.session_state["editor_id"]]
selected_row, state = list(editor_state["edited_rows"].items())[-1]
st.session_state["data"]["select"] = False
if not state["select"]:
st.session_state["last_selected"] = None
return
selected_id = st.session_state["data"].loc[selected_row, ID_COL]
st.session_state["last_selected"] = selected_id
st.session_state["data"].loc[selected_row, "select"] = True
st.session_state["editor_id"] += 1
st.data_editor(
st.session_state["data"],
on_change=edited,
key=st.session_state["editor_id"],
hide_index=True,
column_config={ID_COL: st.column_config.Column(disabled=True)},
)
st.write("Last selected id", st.session_state["last_selected"])
Thanks and sorry for late replay.
Yes it works fine
I just have another question about the text format in data_editor. the long texs are presented as one row. Is there any option with “data_editor” to format output ( wrap text) like this: st.markdown(df.to_html(escape=False), unsafe_allow_html=True)
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.