How to add a delay to a function/app in Shiny for python?


Would anyone know how to add a delay to a function or app using python shiny? I want to simulate a slow loading function but adding the time.sleep function did not slow my app down as expected

Specifically I added the code below to a sample app on shiny live (including link to live app)

 startTime = time.time()
     for i in range(0, 5):
       # making delay for 1 second
      endTime = time.time()
  elapsedTime = endTime - startTime
  print("Elapsed Time = %s" % elapsedTime)

Example live app



Hi Ian,

Thanks for the example, I think this is an issue with time.sleep() not working in web assembly. I tested your app out locally and it behaved as expected (long delay after loading histogram). So the options are:

  • Use the non-shiny live approach with time.sleep() to show the app taking time and share with something like
  • Make your component do actual computation work, for example here I just did the np.random a bunch of times which generates the expected shinylive app.
from shiny import App, render, ui

# Import modules for plot rendering
import numpy as np
import matplotlib.pyplot as plt
import time

app_ui = ui.page_fluid(
            ui.input_slider("n", "N", 0, 100, 20),

def server(input, output, session):
    @render.plot(alt="A histogram")
    def histogram():

        for i in range(0, 500000):
            x = 100 + 15 * np.random.randn(437)

        plt.hist(x, input.n(), density=True)

app = App(app_ui, server, debug=True)
1 Like

Very interesting, I hadn't thought of testing locally vs. shiny live! Worth remembering in future

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.