Wirg / stqdm

stqdm is the simplest way to handle a progress bar in streamlit app.
https://github.com/Wirg/stqdm
Apache License 2.0
248 stars 5 forks source link

[BUG] Nested loops break #101

Open DSLituiev opened 1 month ago

DSLituiev commented 1 month ago

Describe the bug

Nested stqdm loops fail

To Reproduce

stqdm_bug.py:


import streamlit as st
from functools import partial
from stqdm import stqdm
from time import sleep
import logging

progress = partial(stqdm, st_container=st.sidebar)

def subfun(total):
    for n in progress(range(total), desc="paragraph"):
        sleep(0.1)
        pass
sections = "ABCDEFGHIJK"

with progress(total=len(sections), desc="Working on") as pbar:
    for section_counter, draft_section in enumerate(sections):
        logging.warning(f"counter: {section_counter} / {len(sections)}")
        try:
            pbar.update(section_counter)
        except Exception as ee:
            logging.warning(ee)
        pbar.set_description(desc=f"Section '{draft_section}'")
        subfun(30)
        sleep(0.5)
streamlit run stqdm_bug.py

Fails while displaying:

Section 'F': : 15it [00:04, 3.24it/s]

Expected behavior

I expect the nested progress bars to run bug-free as tqdm does

Screenshots

raceback (most recent call last):
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/exec_code.py", line 88, in exec_func_with_error_handling
    result = func()
                  ^^^^^^
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 590, in code_to_exec
    exec(code, module.__dict__)
  File "~/Library/CloudStorage/OneDrive-JNJ/repos/psts-doc-qc/stqdm_bug.py", line 16, in <module>
    with progress(total=len(sections), desc="QC-ing") as pbar:
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/tqdm/std.py", line 1140, in __exit__
    self.close()
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/stqdm/stqdm.py", line 109, in close
    super().close()
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/tqdm/std.py", line 1302, in close
    self.display(pos=0)
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/stqdm/stqdm.py", line 97, in display
    self.st_display(**self.format_dict)
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/stqdm/stqdm.py", line 89, in st_display
    self.st_progress_bar.progress(n / total)
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/streamlit/elements/progress.py", line 146, in progress
    progress_proto.value = _get_value(value)
                           ^^^^^^^^^^^^^^^^^
  File "~/Library/r-miniconda-arm64/dspy/lib/python3.11/site-packages/streamlit/elements/progress.py", line 74, in _get_value
    raise StreamlitAPIException(
streamlit.errors.StreamlitAPIException: Progress Value has invalid value [0.0, 1.0]: 1.363636

if I replace stqdm with tqdm, it works with no issues.

Desktop:

DSLituiev commented 1 month ago

actually this breaks even without nesting. Something about pbar.update()

import streamlit as st
from functools import partial
from stqdm import stqdm
from time import sleep
import logging

progress = partial(stqdm, st_container=st.sidebar)

sections = "ABCDEFGHIJK"

with progress(total=len(sections), desc="Working on") as pbar:
    for section_counter, draft_section in enumerate(sections):
        logging.warning(f"counter: {section_counter} / {len(sections)}")
        try:
            pbar.update(section_counter)
        except Exception as ee:
            logging.warning(ee)
        sleep(0.5)