I have a rather large app.py
file, so I'd like to take a nav panel out and store it in a separate file. I'm not sure how to access code from a different file and include it in the main app.
The app.py
file:
import page
from shiny import reactive, render, req, ui
from shiny.express import input, ui
with ui.navset_hidden(id="selected_navset_tab"):
# Homepage
with ui.nav_panel("Welcome", value="page_home"):
with ui.card():
# Variable calculated
ui.input_selectize(
"filler", "Filler",
["list", "of", "items", "here"],
multiple=False
)
The other file, page.py
:
from shiny import reactive, render, req, ui
from shiny.express import input, ui
with ui.nav_panel("Page I want to put in the main app", value="other_page"):
@render.express
def filler_text():
"Filler text"
How can I import the other_page
nav panel to show as part of the navset_tab in the main app.py
file, without actually putting it in the code?
You can wrap the other_page
nav panel into a function and use this function inside the main app. However, doing this results in an empty additional nav panel and you need to apply express.expressify
. This is because otherwise only the return value of the nav function is shown (None
), where we instead want to display the result of each line (see the Programming UI section within the docs for more details).
from shiny import render
from shiny.express import ui, expressify
@expressify
def otherNav():
with ui.nav_panel("Page I want to put in the main app", value="other_page"):
@render.express
def filler_text():
"Filler text"
import page
from shiny import ui
from shiny.express import input, ui
with ui.navset_tab(id="selected_navset_tab"):
# Homepage
with ui.nav_panel("Welcome", value="page_home"):
with ui.card():
# Variable calculated
ui.input_selectize(
"filler", "Filler",
["list", "of", "items", "here"],
multiple=False
)
# other Nav
page.otherNav()