Skip to content

Instantly share code, notes, and snippets.

@treuille
Created October 30, 2019 22:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save treuille/e8f07ebcd92265a68ecec585f7594918 to your computer and use it in GitHub Desktop.
Save treuille/e8f07ebcd92265a68ecec585f7594918 to your computer and use it in GitHub Desktop.
Workaround: Selecting rows from a dataframe.
import streamlit as st
import pandas as pd
# Load some example data.
DATA_URL = \
"http://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz"
data = st.cache(pd.read_csv)(DATA_URL, nrows=1000)
# Select some rows using st.multiselect. This will break down when you have >1000 rows.
st.write('### Full Dataset', data)
selected_indices = st.multiselect('Select rows:', data.index)
selected_rows = data.loc[selected_indices]
st.write('### Selected Rows', selected_rows)
@treuille
Copy link
Author

This gist gives a workaround to select rows from a table. You can test it out by running:

streamlit run https://gist.githubusercontent.com/treuille/e8f07ebcd92265a68ecec585f7594918/raw

And you should see something like this:

@dariushazimi
Copy link

super cooooooool

@treuille
Copy link
Author

Thanks @dariushazimi!

@acidicph
Copy link

If I want to select only one row and make that its own df that I can populate some dropdown menus and sliders, should I just say:
st.select?

@acidicph
Copy link

Another way to put it, can I select a row that will populate dropdown menus and sliders?

@treuille
Copy link
Author

@acidicph: I‘d use a select box for that. Please also not that “row-selectable dataframe” will be on of our launch examples for custom components when that comes out. Have fun building apps! 🥳

@acidicph
Copy link

Hi, again! Is there a way to input instead of selecting a row based on certain ID because my dataframe is a 100k row

@treuille
Copy link
Author

treuille commented Jun 10, 2020 via email

@acidicph
Copy link

@treuille I think I got around that by letting user input an ID and grabbing the whole row, I think selecting might be a bit too much given that I have a100k of them. My goal is to let the user simulate based on new input that they can adjust and that is passed through a ML model that is pickled along the code. I am having trouble with the part where after grabbing a row from the data frame, "populating" it in a slider and whatnot, as soon as I change it to a new value, the page restarts back. Here is a snippet of my code, could you please guide me onto how to solve this:

st.subheader("Simulate")
    file_name = "train_input_test.csv"
    input_id = st.text_input("Enter ID", "")

    if st.button("Search by Id"):
        IDS = [input_id]
        with open(file_name, newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            rows = [row for row in reader if row['encounter_ID'] in IDS]
        if rows:
            #st.write(rows)
            new_time_in_hospital = st.slider("Time in hospital",1,14,int(rows[0]['time_in_hospital']),None,None)
            new_num_medications = st.slider("Number of Medication",1,81,int(rows[0]['num_medications']))
            new_num_procedures = st.text_input("time_in_hospital", rows[0]['num_procedures'])

            if st.button("Generate New CSV"):
                new_file_name = "new" + file_name
                out_put_file = writer(open(new_file_name, "w"), newline='')

                with open(file_name, "r") as f:
                    read = csv.reader(f)
                    for new_row in read:
                        st.write(new_row)
                        if new_row['encounter_ID'] in IDS:
                            new_row['time_in_hospital'] = new_time_in_hospital

                        out_put_file.writerow(new_row)

@acidicph
Copy link

Right now, you need to use a separate GUI element to select the row, which is what this gist shows. When we get the row-selectable dataframe, you will be able to select directly. If you have further questions about how to solve this scenario via Streamlit I would suggest the forums (discuss.streamlit.io) where a lot of people besides me can help you out! :)

On Wed, Jun 10 2020 at 12:58 PM, Ebrahim H Ghazvini Zadeh < @.*** > wrote: @.**** commented on this gist. Hi, again! Is there a way to input instead of selecting a row based on certain ID because my dataframe is a 100k row — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub ( https://gist.github.com/e8f07ebcd92265a68ecec585f7594918#gistcomment-3337540 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AAMYONLF7MSMRWYOJ6U2KDDRV7QXBANCNFSM4JHBEHTA ).

Alright, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment