Ah I understand what you’re asking now. I believe this example does not work because code = text*10_000 is invalid. If you had a different example with a st.code(code) line that takes a while to execute but is still valid, I’d expect the spinner to appear.
Wouldn’t multiplying a string * 10,000 still give you something invalid, though?
Hmm I believe @MarcoGorelli wanted to pretend he was creating a very long code string for the sake of example. To do that, he’s pretending to define a function 10 000 times, which in terms of code, is accurate, but then feeding it to st.code takes a whole lot of time to print.
I just tested it, and if I preface the the code block with sleep, the spinner keeps running while the code block is compiling. time.sleep(.1) wasn’t enough time to get the spinner initialized, but time.sleep(1) was. So this worked even with a long chunk of code.
code_block = '''
def foo():
print("I see foo.")
'''
with st.spinner('loading code...'):
time.sleep(1)
st.code(code_block*1000)
st.empty and st.container also have some weird bugs where they don’t update quite right when changes run by them too quick. I wonder if there’s some related root issue that just gets the front and back end a little out of sync without forcibly pausing at times to make sure processes catch up…
(When I use a factor of 1000, it takes about 14 seconds to load. When I use a factor 2000, it takes about 50 seconds to load. 3000: 120 seconds. That is of course doing the block building in addition to rendering within the loop though. I do think a factor of 10k would require a piece-wise approach like @arnaud mentioned.)