Streamlit have a df where only a single row can be clicked on and it redirects the user to another page where they can change the values for the specific row and submit. The rows should be clickable themselves.
To upload files, please first save the app
import streamlit as st
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
engine = create_engine("sqlite:///mydb.sqlite")
class Base(DeclarativeBase):
pass
# Define your model here, e.g.,
# class MyModel(Base):
# __tablename__ = 'my_table'
# id = Column(Integer, primary_key=True)
# name = Column(String)
# value = Column(Integer)
Base.metadata.create_all(bind=engine)
@st.cache
def load_data():
return pd.DataFrame({
'id': [1, 2, 3],
'name': ['Item A', 'Item B', 'Item C'],
'value': [10, 20, 30]
})
def main():
st.title('Editable DataFrame Example')
df = load_data()
# Display table
selected_row = st.dataframe(df, on_select="rerun", selection_mode="single-row")
if selected_row:
row_id = selected_row.selection['id']
edit_page(row_id)
def edit_page(row_id):
st.header('Edit Row')
df = load_data()
row_data = df.loc[df['id'] == row_id].iloc[0]
name = st.text_input('Name', row_data['name'])
value = st.number_input('Value', value=row_data['value'])
if st.button('Submit'):
with Session(engine) as session:
# Update the corresponding row in database
# e.g.,
# item = session.get(MyModel, row_id)
# item.name = name
# item.value = value
# session.commit()
st.success('Row updated successfully.')
if __name__ == '__main__':
main()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?