I have been trying, unsuccessfully, to reproduce this minimal R Shiny app in PyShiny. I tried to literally translate the R code to python code, but it looks like I need to dynamically name the plotname() function inside the @render.plot
decorator. All my attempts do not return any plot. I know the problem is with the exec() line near the bottom. How do I dynamically change the function name in the loop? Any help on fixing the code below:
from shiny import App, render, ui
import matplotlib.pyplot as plt
#========== helper function =======================
def do_call(what, args=[], kwargs = {}): # code picked from https://stackoverflow.com/questions/38722804/equivalent-to-rs-do-call-in-python
return what(*args, **kwargs)
#=================================================
max_plots = 5
app_ui = ui.page_fluid(
ui.input_slider("n", "Number of plots", value=1, min=1, max=5),
ui.output_ui("plots")
)
def server(input, output, session):
@output
@render.ui
def plots():
plot_output_list = []
for i in range(1, input.n()+1):
plotname = f"plot{i}"
plot_output_list.append(ui.output_plot(plotname))
return do_call(ui.TagList, plot_output_list)
for j in range(1, max_plots+1):
#my_i = j
#plotname = f"plot{my_i}"
@output
@render.plot
def exec(f"plot{j}")(): # this line is not correct
fig = plt.plot(range(1, j+1), range(1, j+1))
return fig
app = App(app_ui, server)