posit-dev / py-shiny

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

Provide a way for Express app to run/import another file in Express mode? #1102

Open wch opened 8 months ago

wch commented 8 months ago

Summary: It could be useful to have something like @expressify(), but for imports instead of functions.

A user might want to break a Shiny Express app into multiple files, so the app.py looks something like this:

## app.py ##
from shiny.express import ui

ui.h1("Hello")

import section1

ui.hr()

import section2

Where section1.py and section2.py contain Shiny Express code.

However, this won't work because the import won't evaluate section1 or section2 as Express, displaying contents as it goes.

To allow this, we could export the run_express function. It would also require a few modifications so that it doesn't try to alter any top-level state. The code would then look like this:

## app.py ##
from shiny.express import ui

ui.h1("Hello")

run_express("section1")

ui.hr()

run_express("section2")

It may be possible for it to return the values from running the code, so that you could do:

section1  = run_express("section1")
wch commented 7 months ago

Update: this may not be necessary because we already have:

And soon we will have modules that work with Express syntax.

pieterjanvc commented 4 months ago

Hello,

I would like to follow up on @wch comment about modules for Express syntax. I can see there is a module decorator now for Express, but I doesn't seem to be able to return anything. I would like to use it to update a reactive variable in the main app I can then use. Am I missing something, or is the module syntax not complete yet?

Thanks!