Stuck/Hanging on While loop

Summary

I’m using streamlit to provide an easy Web UI interface to interact with python scripts. I am really playing around. The snippet below is from a code where I calc a Bhaskara’s formula.

It runs smoothly on python3 command line on my Ubuntu virtual machine, but when I try to run it with Streamlit, it hangs for around 8 minutes.

If run the entire code, it hangs forever (13+ hours).

What am I doing wrong?

Steps to reproduce

  1. run “streamlit run while.py” on Linux terminal
  2. enter value for etiqueta, like 16 and ensures data was input - no red line surrounding the input field
    3)8+ minutes, the result is shown 4.0

Code snippet:

#! /usr/bin/python3
import streamlit as st
deupau = False
i = 0
calculodelta = st.number_input(label="etiqueta")
while deupau == False:
  i += 1
  divisao = calculodelta / i
  if divisao == i: 
    i2 = divisao 
    deupau = True 
  else: 
    deupau = False
st.write(i2)

Debug info

Full code snippet for reference

#! /usr/bin/python3
import streamlit as st
st.title (“Formula de Bhaskara”)
def run():
st.set_page_config(
page_title=“Formula de Bhaskara”,
page_icon=“”,
)

st.write(“”)
i = 0
deupau = False
st.write(“Formula de Bhaskara”)
st.write(“”)
st.write(“X1=-B+Raiz(Delta)”)
st.write(“--------------------”)
st.write(“2A”)
st.write(“”)
st.write(“X2=-B-Raiz(Delta)”)
st.write(“----------------------------”)
st.write(“2A”)
st.write(“”)
#Imagem01
st.image()
st.write (“Calcula a Bhaska ai pra Noiz!”)
#USER st.number_input
st.write(“Qual é o valor de A?”)
a = st.number_input(“Sugestão 1: " )
st.write(”")
st.write("Valor de A: ", a)

st.write("Qual é o valor de B? “)
b = st.number_input(“Sugestão 8: " )
st.write(””)
st.write("Valor de B ", b)

st.write(“Qual é o valor de C?”)
c = st.number_input(“Sugestão -9: " )
st.write(”“)
st.write(“Valor de C “, c)
#calculodelta
#Imagem 02
st.image(””)
st.write(”“)
st.write(“Calculo do Delta”)
calculodelta = (b * b) - (4 * a * c)
st.write(“Valores previos”)
st.write(“A = “, a)
st.write(“B = “, b)
st.write(“C = “, c)
st.write(””)
st.write(“Delta = B2 - 4AC”)
st.write(“Delta = “,”(”,b,”“,b,”) - (4”,a,”*”,c,”)")
st.write("Delta = ", calculodelta)

#(calculo raiz)
st.write(“”)
st.image(“”)
st.write(“”)
st.write(“Calculo da Raiz”)
#While resultado for diferente de deltaraiz
st.write(“”)
st.write(“Inicio do loop Calculo RAIZ”)

st.write(“Valor Delta:”, calculodelta)
st.write(“Valor I inicial:”, i)
while deupau == False:
i += 1

divisao = calculodelta / i

resultado = divisao * i
if divisao == i:
i2 = divisao
deupau = True
else:
deupau = False

st.write(“”)
st.write(“Valor de I atualizado: “,i)
st.write(“Raiz de Delta: “,i2)
st.write(””)
#imagem 04
st.image(””)
st.write(“”)
st.write(“Calculo X1 e X2”)
st.write(“X1”)
st.write(“X1 = - B + Raiz(Delta)”)
st.write(“----------------------------”)
st.write(“2 * A”)
st.write(“”)
st.write(“X1 = -”,b,’ + ‘,i2)
st.write(“----------------------------”)
st.write(“2 * “, a)
st.write(””)
x1=-(b+i2)/2a
x2=-(b-i2)/2
a
st.write(“X1 = “, x2)
st.write(””)
st.write(“X2”)
st.write(“X2 = - B - Raiz(Delta)”)
st.write(“----------------------------”)
st.write(“2 * A”)
st.write(“”)
st.write(“X2 = -”,b,’ - ',i2)
st.write(“----------------------------”)
st.write(“2 * “, a)
st.write(””)

st.write(“X2 = “, x1)
st.write(””)

#por algum problema de sintaxe python ou logica matematica
#o calculo esta invertendo sinal positivo com negativo
#entao para nao perder mais tempo, inverti as variaveis
#de resultado na impressao do console

st.write(“Olha o veio Baka ae, quer dizer, Bhaskara”)
st.image(“”)
st.write(“”)
st.write(“MAS…”)
st.video(“”)

st.write(“Site de referência”)
st.write(“Fórmula de Bhaskara - Mundo Educação”)
st.write(“Obrigado!! Mafia Dac”)
st.write(“”)

i’ve created a repo with same python scripts on github

Those scripts were added to streamlit cloud and they have same error, so it seems not something related to my internal environment.

Full script - it doesn’t even open on streamlit cloud, internally it does load parcially
https://dacostapiece-dc-cute-st-bhaskara-la4gfb.streamlitapp.com/

Just the while loop script - It gets stuck, but it works (after ±08 minutes internally)
https://dacostapiece-dc-while-zlmt3y.streamlitapp.com/

Full script without while loop - WORKS
https://dacostapiece-dc-looplesscute-st-bhaskara-ve6y59.streamlitapp.com/

It looks like you’re running into an issue with comparing floating-point numbers (which is the default output of st.number_input if you don’t specify otherwise). You can either solve this by setting the value of the number_input to an integer by default, and the step to an integer, or you can just convert the value to an integer after the user puts it in.

For example,

calculodelta = st.number_input(label="etiqueta", value=16, step=1)

Blackary, i appreciate your reply, code is now showing, but i notice, it actually is loading because i set default values for them. When i start changing, the “app” starting running forever again.

I am trying to figure out how to solve it. I am initially thinking of using st.button to enable user input field, setting it as Disabled=True as default, then when some clicks to keep on going, the user input field gets ready to receive info.

For now, once button is no longer being clicked, the name user input dissapears, probably because it’s inside IF condition, but if value is set to True (clicked button), buttonName variable receives True again :slight_smile:

https://dacostapiece-dc-button-0is4f9.streamlitapp.com/

How is this related to your original question? There is no while loop and no hanging for 8 minutes in this snippet.

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