Display entered data from sidebar

Hi all ,please excuse me but i am complete beginner with this. Trying to create simple web application that can be used to order stock for a bar. I have managed to get the List of products from an xls file and then display in dropdown on the sidebar. But i need to create a table in the main window based on products and quantities selected in the sidebar. Anyone that can steer me in the right direction here ?
Thanks

1 Like

Hi @Rynard_Coetzee,

The best way to ask for help is to share your code snippet.

Here are instructions on how to do so
If you could share your code by typing the following into the Reply to this post as shown in the screenshot below:
Screenshot 2566-04-13 at 20.38.32

This would produce a code box like the following:

import streamlit as st

st.write('Hello world!')

This will allow all of us to see exactly how you are implementing your code so that all of us in the community can help out.

Apologies ,see code below :slight_smile:
"from pathlib import Path # Python Standard Library

import pandas as pd # pip install pandas
import plotly.express as px
import streamlit as st

st.set_page_config(page_title=“Stock Order”,
page_icon=“:bar_chart:”,
)

df = pd.read_excel(
io=‘Stock Order Form.xlsm’,
engine=‘openpyxl’,
sheet_name=‘Sheet2’,
skiprows=0,
usecols=‘A:E’,
nrows=100,
)

st.dataframe(df)

st.sidebar.header(“Select Product/Quantity Here:”)
product = st.sidebar.selectbox(
“Select the Product:”,
options=df[“Column1”].unique()
)
quantity = st.sidebar.selectbox(
“Select the Amount:”,
options=df[“Column2”].unique()
)

print(df)"

Hi @Rynard_Coetzee, it is much easier to view and try out the code if you please use triple backticks (```) just like @dataprofessor said, rather than just quotes (")

‘’'from pathlib import Path # Python Standard Library

import pandas as pd # pip install pandas
import plotly.express as px
import streamlit as st

st.set_page_config(page_title=“Stock Order”,
page_icon=“:bar_chart:”,
)

df = pd.read_excel(
io=‘Stock Order Form.xlsm’,
engine=‘openpyxl’,
sheet_name=‘Sheet2’,
skiprows=0,
usecols=‘A:E’,
nrows=100,
)

st.dataframe(df)

st.sidebar.header(“Select Product/Quantity Here:”)
product = st.sidebar.selectbox(
“Select the Product:”,
options=df[“Column1”].unique()
)
quantity = st.sidebar.selectbox(
“Select the Amount:”,
options=df[“Column2”].unique()
)

print(df)‘’’

Hi @Rynard_Coetzee, thanks for the quick response, but can you please go back to your original source code and copy from there? It appears that you’ve copied from your post instead of your source code, which means that, among other things, all of your quotes are “curly quotes” rather than “straight quotes”. See this screenshot something I copied from your posted code – the first line shows what you posted, which is not valid python, and the second line shows what it needs to be. If you always copy directly from your code, and then enclose that in curly quotes, that will ensure that:

  1. The indentation says the same
  2. The quotes don’t get automatically converted to curly quotes

image

All that being said, I think you’ll want to do something like this to show what the user has selected"

filtered = df[(df["Column1"] == product) & (df["Column2"] == quantity)]
st.write(filtered)

Sorry man trying again ,but for more clarity ,in the “Select Product” in my sidebar selectbox which references a list of products from the xls spreadsheet ,i want to select for instance product A and then enter a quantity to order and then when i submit this it should add to a table in the middle of the page ,if that makes sense

from pathlib import Path  # Python Standard Library

import pandas as pd  # pip install pandas
import plotly.express as px
import streamlit as st

st.set_page_config(page_title="Stock Order",
                   page_icon=":bar_chart:",
)

df = pd.read_excel(
    io='Stock Order Form.xlsm',
    engine='openpyxl',
    sheet_name='Sheet2',
    skiprows=0,
    usecols='A:E',
    nrows=100,
  )
  
st.dataframe(df)

st.sidebar.header("Select Product/Quantity Here:")
product = st.sidebar.selectbox(
        "Select the Product:",
        options=df["Column1"].unique()
)
quantity = st.sidebar.selectbox(
        "Select the Amount:",
        options=df["Column2"].unique()
)
  
print(df)

Your code already does this. Maybe the displayed table is not the right one? What table do you want to display then?

1 Like

The idea of the program is to select from a dropdown list the Product to be ordered and then enter a quantity of that product to be ordered ,this should then create a list of products/quantities in the middle of the page ,currently the program sidebar only lists the products

Ok ,i have managed to get closer to what i am trying to achieve by using a form ,problem i now have is that when i select the product from the dropdown and then enter the quantity and lastly select the type ,i click submit ,but it writes the dataframe with the product list and then appends the new entry to the bottom of it. I just want to write the new entries and keep appending to them


import pandas as pd  # pip install pandas
import plotly.express as px
import streamlit as st

st.set_page_config(page_title="Stock Order",
                   page_icon=":bar_chart:",
)

df = pd.read_excel(
    io='Stock Order Form.xlsm',
    engine='openpyxl',
    sheet_name='Sheet1',
    skiprows=0,
    usecols='D',
    nrows=100,
  )
  


st.sidebar.header("Select Product/Quantity Here:")
options_form = st.sidebar.form("options_form")
Product = options_form.selectbox(
        "Select the Product:",
        options=df["Column1"].unique(),
)
quantity = options_form.text_input(
        "Enter the Amount:")
type = options_form.radio(
        "Select type:",('Bottle', 'Box' , 'Case'))
add_data = options_form.form_submit_button()
if add_data:
    new_data = {"Product": Product, "quantity": int(quantity) ,"type": type}
    df = df.append(new_data, ignore_index=True)
    st.write(df)

Ok ,so i managed to get everything working by writing the submitted Data to a .csv file and the reading the data back from the .csv file into the dataframe. Only thing left now is i need a button to clear the .csv file ,is this possible ?

Hi, glad to see that most of the task are already implemented.

To have a button that clears your CSV, you can use conditionals together with the st.button() command.

Perhaps, something like the following to write a blank DataFrame to a CSV file named file.csv:

if st.button('Clear CSV')
   df = pd.DataFrame()
   df.to_csv('file.csv')

Hi ,Thanks for the reply ,really appreciate the help. I managed to get it working using the following code ,not sure if it is structurally correct ,but it works for my purpose

if st.sidebar.button('Clear'):
    df = df[0:0]
    df.to_csv("Order.csv", index=False)
2 Likes

Glad you managed to find a workaround that worked for your use case! :slight_smile:

Thanks, @dataprofessor and @blackary, for stepping in!

Best,
Charly

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.