posit-dev / py-shiny

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

AttributeError: module 'app' has no attribute 'app' #1155

Open dlavrent opened 6 months ago

dlavrent commented 6 months ago

Hello -- I am trying to deploy a basic py-shiny app on an Oracle Cloud virtual machine (aarch64, Ubuntu 22.04) but am running into the following error when I try to access my app on another computer at <my-virtual-machine-ip>:3838/my_shiny_apps/basic-app/

su: ignoring --preserve-environment, it's mutually exclusive with --login
Using Python 3.10.13 at /home/shiny/anaconda3/envs/shiny310/bin/python
Traceback (most recent call last):
  File "/usr/local/shiny-server/python/SockJSAdapter.py", line 204, in <module>
    run()
  File "/usr/local/shiny-server/python/SockJSAdapter.py", line 197, in run
    app = getattr(app_module, "app")
AttributeError: module 'app' has no attribute 'app'

The app.py that I am trying to deploy:

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

ui.panel_title("Hello Shiny!")
ui.input_slider("n", "N", 0, 100, 20)

@render.text
def txt():
    return f"n*2 is {input.n() * 2}"

In case it's useful, my /etc/shiny-server/shiny-server.conf has the following lines pointing to a virtual environment that I am wondering is what is causing the issue:

python /home/shiny/anaconda3/envs/shiny310/;
run_as shiny;

However, if in my virtual machine I activate my virtual environment and run the app locally, shiny run -h 0.0.0.0, then I can access my app from another computer's browser just fine when I connect to <my-virtual-machine-ip>:8000.

I see that someone else has posted this error in recent days as well: https://community.rstudio.com/t/problem-with-deploying-example-app-with-shiny-server-module-app-has-no-attribute-app/181974

I'd appreciate any guidance! This is my first foray into using virtual machines/ARM processors too.

My virtual environment uses python 3.10.13 and shiny 0.7.1.

wch commented 6 months ago

I believe the error message you're seeing is related to the app being a Shiny Express (as opposed to Shiny Core) app.

Just to make sure it has something to do with Shiny Express: Have you tried deploying a Shiny Core app? Here is a basic one you can use:

from shiny import App, render, ui

app_ui = ui.page_fixed(
    ui.input_slider("val", "Slider label", min=0, max=100, value=50),
    ui.output_text_verbatim("slider_val")
)

def server(input, output, session):
    @render.text
    def slider_val():
        return f"Slider value: {input.val()}"

app = App(app_ui, server)
dlavrent commented 6 months ago

Thank you -- I tried deploying a Core version of the basic slider app and it works just fine.

I converted a more complicated Express app to Core and now that works fine too.

wch commented 6 months ago

It seems that Shiny Server Open Source does not yet support for Shiny Express apps. However, you can actually wrap your Shiny Express app so that it looks like a Shiny Core app.

If you name your Shiny Express app file from above express_app.py, and then add the following file as app.py, it should work:

## app.py ##
from pathlib import Path
from shiny.express._run import wrap_express_app

# Create a shiny.App object from the Shiny Express app file
app = wrap_express_app(Path(__file__).parent / "express_app.py")
jcheng5 commented 6 months ago

Shoot, that’s quite an oversight, sorry. I’ll add it to the list.

jcheng5 commented 6 months ago

PR is here: https://github.com/rstudio/shiny-server/pull/566