Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use insert_ui() with more complex ui elements #1695

Open
Bnjmn83 opened this issue Sep 19, 2024 · 1 comment
Open

use insert_ui() with more complex ui elements #1695

Bnjmn83 opened this issue Sep 19, 2024 · 1 comment

Comments

@Bnjmn83
Copy link

Bnjmn83 commented Sep 19, 2024

I'd like to create new UI elements on the fly via a button. It seems if it is more than just a single UI object is not possible. This is the code I would like to get rendered. If I push the button 3 times I want this code to be added 3 times. How can I achieve this?

                with ui.layout_columns(col_widths=(3, 9)):
                    @render.data_frame
                    def render_frame():
                        ...
                        return render.DataGrid(df)
                    
                    @render_plotly
                    def render_plot():
                      ...
                      return fig
@Bnjmn83 Bnjmn83 changed the title use inser_ui() with more complex use insert_ui() with more complex Sep 20, 2024
@Bnjmn83 Bnjmn83 changed the title use insert_ui() with more complex use insert_ui() with more complex ui elements Sep 20, 2024
@ramnathv
Copy link

ramnathv commented Oct 7, 2024

This should do the trick. It does rerender every df_with_plot. It should be possible to update this code to avoid the re-render and only insert the new df_with_plot.

from shiny.express import input, render, ui, output, expressify, module
from shiny import reactive
from shinywidgets import render_plotly
import plotly.express as px

df = px.data.tips()

@module
def df_with_plot(input, output, session):
    with ui.layout_columns(col_widths=(3, 9)):
      @render.data_frame
      def render_frame():
          return render.DataGrid(df)

      @render_plotly
      def render_plot():
        fig = px.scatter(df, x="total_bill", y="tip", height=600)
        return fig

ui.input_action_button("add_ui", "Add UI")

@render.express
def _():
    for i in range(input.add_ui()):
        df_with_plot(str(i))

Shinylive URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants