DuplicateWidgetID

Hi, everybody!

I’m trying to get some data from users into a for, but I have this error: DuplicateWidgetID. My code is below.

lista_ativos = []
df2 = pd.DataFrame()
qtd_ativo = st.number_input("Quantidade de ativos: ")

# Pegando os ativos do usuário e populando a lista lista_ativos
for i in range(int(qtd_ativo)):
    lista_ativos.append(st.text_input("Nome do ativo + .SA: "))
    dt_inicio = st.date_input("Data de início: ")
    dt_fim = st.date_input("Data de fim: ")

The result is:

Thanks for your attention!

1 Like

In this situation, you will need to add a unique key for each widget in your for loop. You can use the variable “i” for this. Look up the key argument in the API (sorry, posting on my phone or I’d look it up for you).

The underlying problem is that each widget you set up in your for loop has identical arguments when called, so streamlit cannot tell them apart.

2 Likes

Note - you’ll have to do this with all the widgets in your loop.

2 Likes

Oh, thanks about this information. I may face an issue. I want that the user choose how many tickers he has. Do you have any solution for this case, specifically?
All that I have in mind is just restrict the quantity of tickers.

Ps.: sorry about my English mistakes, I’m Brazilian and I trying to improve it

1 Like

Your English is fantastic, and far better than my Portuguese.

Each widget in the loop just needs a unique key specifying. This has to be a string. Luckily, you have a loop variable (i) which will be unique for each run through the loop.

For the text widget, for example, use something like:

st.text_input("Nome do ativo + .SA: ",key=str(i))

Note that all that has changed is the addition of key=str(i)

You are allowing the loop to generate the correct number of sets of “tickers”, so this should match the user input.

I suspect that you are next going to run into a problem of recording all the inputs from all the widgets - adding them to a list is probably the correct way, as you do with

lista_ativos.append()

The other two variables dt_inicio and dt_fim are going to keep getting overwritten.

3 Likes

It worked. Thank you a lot!

No probs :wink: