DuplicateWidgetID 2

Hi everyone.

Can you pls help me with python code. I am trying to make an pulp linear programing simplex method. Here is my code:

> import streamlit as st
> import pandas as pd
> from pulp import *
> 
> # Load some example data.
> 
> data1 = st.cache(pd.read_excel)("ISHRANA_PERADI.xlsx", nrows=68, sheet_name='KRMIVA')
> data2 = st.cache(pd.read_excel)("ISHRANA_PERADI.xlsx", nrows=40, sheet_name='POTREBE_PERADI')
> 
> st.write('Odaberite.', data2)
> selected_indices = st.multiselect('Odaberite ishrambeni :', data2.index, key='1')
> selected_rows = data2.loc[selected_indices]
> st.write('### Odabrani ishrambeni program:', selected_rows)
> row_index = selected_indices
> col_index = st.selectbox("Odaberite ogranicenje :", data2.columns, key='2')
> 
> 
> st.write('Odaberite željena Krmiva.', data1)
> selected_indices = st.multiselect('Odaberite redove:', data1.index, key='3')
> selected_rows = data1.loc[selected_indices]
> st.write('### Odabrani redovi, odnosno:', selected_rows)
> 
> new_data = pd.DataFrame(selected_rows)
> 
> filtered = st.multiselect("Odaberite parametre:", options=list(new_data.columns), default=list(new_data.columns), key='4')
> 
> st.write(new_data[filtered])
> new_data2 = pd.DataFrame(filtered)
> 
> decision_variables = []
> for rownum, row in new_data.iterrows():
>     variable = str('x' + str(rownum))
>     variable = pulp.LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Integer')
>     decision_variables.append(variable)
> 
> print ("Ukupan_broj_decision_variabli: " + str(len(decision_variables)))
> print(decision_variables)
> 
> # define the problem
> model = pulp.LpProblem('test',LpMinimize)
> 
> objektivnaf = ""
> for rownum, row in new_data.iterrows():
>     for i, schedule in enumerate(decision_variables):
>         print(schedule)
> 
>         if len(str(schedule)) == 2:
>             print(str(schedule)[1])
>             a = str(schedule)[1]
>             broj = int(a)
> 
>         if len(str(schedule)) > 2:
>             print(str(schedule)[1])
>             print(str(schedule)[2])
>             a = str(schedule)[1]
>             b = str(schedule)[2]
>             broj = int(a + b)
> 
>         if rownum == broj:
>             print("ispunjen")
>             formula = row['Cijena']*schedule
>             objektivnaf += lpSum(formula)
>             model += objektivnaf
> 
> print("Optimization function: " + str(objektivnaf))
> st.title("Funkcija je: ")
> st.title(objektivnaf)
> 
> def racun(model, key):
>     ogra1 = ""
>     relation = st.selectbox(f"Odaberite znak {key}:", options=["==","<=", ">="], key="relat")
> 
>     if relation == "<=":
> 
>         if 'ME' in filtered:
>             for rownum, row in new_data.iterrows():
>                 print(row)
> 
>                 for i, schedule in enumerate(decision_variables):
>                     print(schedule)
> 
>                     if len(str(schedule)) == 2:
>                         print(str(schedule)[1])
>                         a = str(schedule)[1]
>                         broj = int(a)
> 
>                     if len(str(schedule)) > 2:
>                         print(str(schedule)[1])
>                         print(str(schedule)[2])
>                         a = str(schedule)[1]
>                         b = str(schedule)[2]
>                         broj = int(a + b)
> 
>                     if rownum == broj:
>                         print("ispunjen")
>                         cell_value = data2.loc[row_index, col_index]
>                         print(f"The value at row {row_index} and column {col_index} is {cell_value}")
>                         st.write(cell_value)
> 
>                         ogra1 = lpSum(row['ME']*schedule) <=cell_value
>                         model += ogra1
> 
>             print("Funkcija Ogranicenja: " + str(ogra1))
>             st.write("Jednačina Ograničenja je: ", str(ogra1))
> 
>     return ogra1
> racun(model, 5)
> 
> opci = ["DA", "NE"]
> novagr = st.radio("Odaberite", opci)
> if novagr == "DA":
>     racun(model, 5)

when I run it I got this error:

DuplicateWidgetID : There are multiple identical st.selectbox widgets with key='relat' .
```
File “C:\Users\BH TELECOM\Documents\Adnan\Kursevi\lineaf\nastavak10.py”, line 112, in

  • racun(model, 5)File “C:\Users\BH TELECOM\Documents\Adnan\Kursevi\lineaf\nastavak10.py”, line 71, in racun*
  • relation = st.selectbox(f"Odaberite znak ogranicenja {key}:“, options=[”==“,”<=", “>=”], key=“relat”)*
    ```

How to solve this? What could be a problem? thx in advance everyone.

Hi @Adnan_Adnan,
Try changing:
key=“relat”
to
key=key

Cheers

Hi Shawn.

No I got error: DuplicateWidgetID: There are multiple identical st.selectbox widgets with key='1'.

To fix this, please make sure that the key argument is unique for each st.selectbox you create.

what ever I put for key that appears in error as abowe

Have a look at this blog post:

Streamlit doesn’t allow multiple widgets with the same key on the same page. If a key isn’t given, then the widget type and title effectively act as the key. Use a debug stepper to find where identical widgets are being created. As a last resort use a randomly generated key to force the widgets to display and then visually inspect the rendered page to see where the widget is being displayed multiple times.

2 Likes

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