Python Shiny:如何使用两个按钮切换条件面板的可见性?

huangapple go评论55阅读模式
英文:

Python Shiny: How to toggle visibility of a conditional panel with two buttons?

问题

Python Shiny:通过按钮点击打开和关闭条件面板

在我的 Web 应用程序中,我想通过按钮点击来打开一个条件面板。在该面板上,我想添加一个关闭按钮以再次关闭它。下面我将添加一些代码片段,展示我尝试过的内容:

首先,我尝试了这个方法:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "input.show",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ),
)


def server(input: Inputs, output: Outputs, session: Session):
    pass


app = App(app_ui, server)

使用这种方法,我可以打开面板,但无法再次关闭它。这是因为关闭按钮还没有功能。

第二次尝试:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "close_panel",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.Effect
    def close_panel():
        value = False

        if input.show():
            value = False
        if input.close():
            value = True

        return value


app = App(app_ui, server)

有人知道如何使用两个按钮来打开和关闭面板吗?提前感谢您:)。

英文:

Python Shiny: Open and close panel_conditional through button press

In my webapplication I want to open a conditional panel through a button click. On that panel I want to add a close button to close it again. Below I will add some codesnippets on what I have tried:

First I tried this:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "input.show",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ),
)


def server(input: Inputs, output: Outputs, session: Session):
    pass


app = App(app_ui, server)

With this method I can open the panel but not close it again. This makes sense because the close button does not have a function yet.

Second attempt:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "close_panel",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ),
)


def server(input: Inputs, output: Outputs, session: Session):
    @reactive.Effect
    def close_panel():
        value = False

        if input.show():
            value = False
        if input.close():
            value = True

        return value


app = App(app_ui, server)

Does somebody know a way how to open and close the panel by using two buttons? Thank you in advance :).

答案1

得分: 1

  1. 你的panel_conditional有错误的条件输入值,依赖于另一个UI,比如你的ui.input_action_button,其id为show的标签。你可以使用input.show来让这两个UI特性相互关联。

  2. ui.input_action_button的工作方式是一个从0开始的计数器,每次点击都会将计数器加1。例如,input.close的值将从0开始,每次点击都会增加1;通常,使用UI的人会先点击open,然后再点击close,因此open的值应该始终大于close的值,如果不是,那么条件将不满足。

  3. 在你的def server()中的值没有在你的UI中使用。要使用它,你需要一个具有ui.update_____类型函数的UI元素,比如ui.update_action_button或类似的函数。

  4. 这个解决方案没有考虑到一个边缘情况,即人们可能会多次点击show按钮,这种情况下需要点击close按钮相同次数。

  5. 为了更好地理解input.show和input.close的输出,请将以下内容放入def server()中:

@reactive.Effect 
def _():
    print(input.show(), input.close())

以下是解决方案的代码:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "input.show > input.close",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ),
)

def server(input, output, session):
    pass

app = App(app_ui, server)
英文:
  1. Your panel_conditional has the wrong condition input value dependent on another ui such as you ui.input_action_button labeled with id=show. You can grab input.show to let those two UI features be linked to each other
  2. The way the ui.input_action_button works is that it is a counter that starts at 0 and each click iterates the counter by 1. For example the value of the input.close will start at 0 and every click will increment it by 1; Normally, the person using the UI would click on open first and then close so the value of open should always be greater than the value of close and if it is not, then it will fail the condition
  3. The value in your def server() does not use that value in your UI. To use it, you will require a UI element that has an ui.update_____ type of function such as ui.update_action_button or so
  4. This solution does not take into account the edge case that the person may click on the show button more than once, in which case will require the person to click on the close button an equal number of times
  5. To view the output of input.show and input.close for better understanding, put this into the def server():

.

@reactive.Effect 
def _():
    print(input.show(), input.close())

Here is the solution:

app_ui = ui.page_fluid(
    ui.input_action_button("show", "Show"),
    ui.panel_conditional(
        "input.show > input.close",
        ui.input_slider("slider", None, min=0, max=100, value=50),
        ui.input_action_button("close", "Close")
    ), )


def server(input, output, session):
    pass


app = App(app_ui, server)

huangapple
  • 本文由 发表于 2023年5月24日 18:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定