Spinner while waiting for `st.code`?

Summary

Can I show a spinner for when an expensive st.code operation is running?

Steps to reproduce

Code snippet:

import streamlit as st

text = (
    'def foo():\n'
    '    pass\n'
)
code = text*10_000

with st.spinner('loading code...'):
    st.code(code)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

I’d like some spinner to show whilst st.code is running

Actual behavior:

This is displayed for quite a long time:

Debug info

  • Streamlit version: 1.16.0
  • Python version: 3.8.16
  • Using Conda? PipEnv? PyEnv? Pex? pip
  • OS version:
  • Browser version:
1 Like

Hi @MarcoGorelli,

What’s the goal with the st.code piece? st.code is meant to just display code (it doesn’t run your code) – check out the doc here.

Yes, and I want it to display a big code file

However, it takes a long time for it to load (unless I set language=None, it seems that the syntax highlighting is very slow)

If you run the example I posted above you’ll see what I mean, it takes quite a while just to display the code

What is the expected behavior from this line? code = text*10_000

That was just to make a reproducible example in which the input to st.code is a very long string

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.

In what sense is code = text*1000 invalid? It works, it just takes a long time, and the spinner doesn’t show up

This works fine for me:

code_block = '''
def foo():
    print("I see foo.")
'''

with st.spinner('loading code...'):
    time.sleep(3)
    st.code(code_block)

Is your code block so very large that it exceeds the memory limit?

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 couldn’t make the spinner work, but did you try chunking the long piece of code into smaller pieces @MarcoGorelli? Here’s an example, it’s not ideal but looks much faster to me https://playground.streamlit.app?q=split-code-in-chunks

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.)

Um, no? You can try and see, the example I posted is reproducible :smile:


Thanks @mathcatsand ! In the end I think I’ll just set language=None, that speeds things up significantly

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