posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.32k stars 82 forks source link

@reactive.event cause RuntimeError: shiny.express.input can only be used inside of a Shiny Express app. #1741

Open wdesaintjean opened 1 month ago

wdesaintjean commented 1 month ago

Hello I wrote an app in shiny express python, and its worked well in a local server on my computer. But when I try to install my app on a server, (with a docker image, and irequired python packages), I have an error that said:

Traceback (most recent call last):
  File "/home/app/app.py", line 197, in <module>
    @reactive.event(input.action_button)
  File "/usr/local/lib/python3.10/site-packages/shiny/express/__init__.py", line 76, in __getattr__
    raise RuntimeError(
RuntimeError: shiny.express.input can only be used inside of a Shiny Express app.

It seems to not accept the utilization of @reactive.event() in a shiny express application, but I saw in the documentation that the reactive module is unchanged between Core and Express. I don't understand the problem. Here the problematic part of my code:

from shiny import reactive
from shiny.express import input, render, ui

ui.panel_title("Exploration of Nanopore RNAseq results")

ui.input_text("gene_name", "Gene name Symbol", "Enter gene")   

ui.input_action_button("action_button", "Search", disabled=False)

@render.text 
@reactive.event(input.action_button)
def txt():

    gene_conv=pl.read_csv("./data/geneToSymbol_GRCm38.99.csv", separator="\t", has_header = False)
    if input.gene_name() in gene_conv['column_2']:
        return f"The chosen gene is {input.gene_name()}"
    else: 
        return f"The gene symbol is not in mm10 reference"

What's bug me is that worked perfectly fine on my computer with manual import of python packages. I suppose I can always convert my code in Shiny Core python, but it will be a lot of works, I will gain a lot of time if It 's only a little mistake.

Can you help me?

GiorgioMiglia commented 1 month ago

I ran your code in a devcontainer with Python 3.12.4 and Shiny, and it worked without any issues. This suggests the problem might not be with this snippet specifically, but rather with the overall structure of your code.

My test: App.py

from shiny import reactive
from shiny.express import input, render, ui

ui.panel_title("Exploration of Nanopore RNAseq results")

ui.input_text("gene_name", "Gene name Symbol", "Enter gene")   

ui.input_action_button("action_button", "Search", disabled=False)

@render.text 
@reactive.event(input.action_button)
def txt():

    #gene_conv=pl.read_csv("./data/geneToSymbol_GRCm38.99.csv", separator="\t", has_header = False)
    if input.gene_name() in ['ok', 'ok2']:
        return f"The chosen gene is {input.gene_name()}"
    else: 
        return f"The gene symbol is not in mm10 reference"

devcontainer.json

{
    "name": "test",
    "build": {
      "dockerfile": "Dockerfile",
      "context": ".."
    },
    "customizations": {
    "vscode": {
      "extensions": [
          "ms-python.python",
        "Posit.shiny",
        "ms-azuretools.vscode-docker",
        ],
      "settings": {"python.defaultInterpreterPath": "/usr/local/bin/python"}
    }
  }
  }

Dockerfile:

FROM python:3.12.4

RUN apt-get -y update && apt-get -y upgrade

WORKDIR /

RUN pip install shiny

COPY . .
wdesaintjean commented 4 weeks ago

Thank you for your answer, I rewrite my code only in shiny core and its working. The problem can came from our installation on the server or from the python version used ( 3.10) I will investigate.

Thank you again for testing my code.