switch ui

Published by onesixx on

from shiny import App, Inputs, Outputs, Session, reactive, render, ui

app_ui = ui.page_sidebar(
    ui.sidebar(
        ui.input_switch("nemo_swh_show", "Duration", False),
        ui.output_ui("nemo_switchUI"),
        ui.input_action_button("nemo_btn_fetch", "Fetch"),
    ),
    ui.navset_tab(
        ui.nav_panel("Overview", )
    )
)

def server(input: Inputs, output: Outputs, session: Session):
    @render.ui
    @reactive.event(input.nemo_swh_show)
    def nemo_switchUI():
        if input.nemo_swh_show() :
            return ui.TagList(
                ui.input_date_range("nemo_drng", "Select Dates :"),
            )
        else:
            return ui.TagList(
                ui.input_date("nemo_date", "Select Dates :"),
            )

app = App(app_ui, server)

shiny적용

from shiny import App, Inputs, Outputs, Session, reactive, render, ui
from pydataset import data
import logging
from datetime import datetime, timedelta
# for data_name in list(data()['dataset_id']):
#     print(data_name)
#     print(data(data_name))
# economics, presidential, 1967-06-30 2007-03-31

logger = logging.getLogger("shiny_log")
logging.basicConfig(level=logging.INFO,
    format = '====>%(levelname)s|%(message)s'
)

app_ui = ui.page_sidebar(
    ui.sidebar(
        ui.input_switch("nemo_swh_show", "Duration", False),
        ui.output_ui("nemo_switchUI"),
        ui.input_action_button("nemo_btn_fetch", "Fetch"),
    ),
    ui.navset_tab(
        ui.nav_panel("Overview",
            ui.br(), 
            ui.output_data_frame("nemo_df_overview")
        ),
    )
)

def server(input: Inputs, output: Outputs, session: Session):
    @render.ui
    @reactive.event(input.nemo_swh_show)
    def nemo_switchUI():
        if input.nemo_swh_show() :
            return ui.TagList(
                ui.input_date_range("nemo_drng", "Select Dates :",
                    start="1967-06-30", end="1967-10-31",
                    min="1967-06-30", max="2007-03-31",startview="year"),
            )
        else:
            return ui.TagList(
                ui.input_date("nemo_date", "Select Dates :", 
                    value="1967-06-30", 
                    min="1967-06-30", max="2007-03-31", startview="year"),
            )
    
    @render.data_frame
    @reactive.event(input.nemo_btn_fetch)
    def nemo_df_overview():
        df = data("economics").copy()
        df["date"] = df["date"].apply(lambda x: datetime.strftime(x, "%Y-%m-%d"))

        if input.nemo_swh_show():
            start_date = input.nemo_drng()[0]
            end_date = input.nemo_drng()[1]
            all_dates = []
            while start_date <= end_date:
                all_dates.append(start_date)  
                start_date += timedelta(days=1)
        else:
            all_dates = [input.nemo_date()]
        # all_dates = [datetime.strftime(i, "%Y-%m-%d") for i in all_dates]

        logging.info(f"all_dates: {all_dates}")
        df = df[df["date"].isin(all_dates)]
        return render.DataGrid(df)

app = App(app_ui, server)
Categories: shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x