Giter Club home page Giter Club logo

Comments (2)

wch avatar wch commented on August 17, 2024

The behavior for relative imports can be a little confusing. It stems from how uvicorn handles relative imports (or doesn't, depending on how you launch the app).

First, when you use shiny run, there are four different ways you could launch an app::

  1. shiny run app.py: launching it as a file from the app's directory
  2. shiny run myapp/app.py: launching it as a file from the app's parent directory
  3. shiny run app:app: launching it as a module from the app's directory
  4. shiny run myapp.app:app: launching it as a module from the app's parent directory

For the first two, shiny run internally does something like the following: it goes to the app directory, and then runs uvicorn run app:app. For the latter two, it basically just calls uvicorn run app:app or uvicorn run myapp.app:app.

With that background, for Shiny Core apps:

  • If you launch it from the app directory with uvicorn app:app, then the relative import won't work.
  • If you launch it from the parent directory with uvicorn myap.app:app, then the relative import will work.

Shiny Express are a bit different, because they are actually wrapped into a Shiny Core app. For Shiny Express, we recently added the ability to use relative imports. However, for Shiny Express apps, you can only launch apps as files, not as modules. So you can do (1) shiny run app.py and (2)shiny run myapp/app.py, but not (3) shiny run app:app or (4) shiny run myapp.app:app. The reason for this is because Shiny Express needs to evaluate the code in a special way to capture the result of each line, similar to how Jupyter notebooks evaluate the code. We can't just pass app:app to uvicorn and have it work; we actually need to wrap up the code in a special evaluator inside of a Shiny Core app, and then pass that Shiny Core app to uvicorn.

If you want to launch a Shiny Express app from Python (instead of from the command line), it is possible, but I won't go into detail right here.

The PR that added this functionality to Shiny Express is #1464. That PR has some other information that may be useful.

This spreadsheet shows in which conditions you can do which kind of import (apologies if it is somewhat confusing to read): https://docs.google.com/spreadsheets/d/1jIVoEDr234uo_ALOPiilSIoU-bj18L-R6YP_39ORXwM/edit?gid=0#gid=0

image

It looks like kind of a mess, and again, that's in part because of how uvicorn handles relative imports.

We generally expect people to:

  • Use global imports like import utils, instead of relative/submodule imports, like from . import utils.
  • Launch their apps as files, with shiny run app.py or shiny run myapp/app.py.

If you do those things, the imports should work.

from py-shiny.

frere-jacques avatar frere-jacques commented on August 17, 2024

Okay, I see then, why it works with run and not with the app fixture.

Is there any other way to create the fixture from running the module instead of linking to the py file?

If not then just using the absolute path might work to have a testable package.

from py-shiny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.