I have a streamlit app that download automatically one file at the end of each loop iteration. The problem is that at the end of each iteration, the code also adds a few empty spaces. I tried to remove the spaces, but it did not work. Can someone maybe help, please?
This is unfortunately expected. To work around it, you could use an empty to replace the script instead of append it each time. Alternatively, you could use a fragment which would automatically replace itself by default.
In addition to either solution, you could also use containers to make sure the script is always at the end of the page.
Note: You’d just need to make sure at least one character was different in what you were injecting so that the script will execute. If you don’t have something about the script that changes with each iteration, include a timestamp comment, for example.
import streamlit as st
from streamlit.components.v1 import html
from datetime import datetime
if "run_every" not in st.session_state:
st.session_state.run_every = 0
@st.fragment(run_every=1)
def run_script():
script = f"<p>Inject some script {datetime.now()}</p> "
html(script)
body = st.container()
footer = st.container()
if body.button("Start"):
with footer:
run_script()
with body:
st.button("Stop")
"Whatever comes after"