Skip to content

Commit 2c2b075

Browse files
authored
feat: Add theme argument to page functions (#1334)
1 parent 59b1f13 commit 2c2b075

13 files changed

+391
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
* Added support for creating modules using Shiny Express syntax, and using modules in Shiny Express apps. (#1220)
1515

16+
* `ui.page_*()` functions gain a `theme` argument that allows you to replace the Bootstrap CSS file with a new CSS file. `theme` can be a local CSS file, a URL, or a [shinyswatch](https://posit-dev.github.io/py-shinyswatch) theme. In Shiny Express apps, `theme` can be set via `express.ui.page_opts()`. (#1334)
17+
1618
### Bug fixes
1719

1820
### Other changes

shiny/api-examples/theme/_purgecss.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# This file is used to create a minimal Bootstrap CSS file based on the Minty
2+
# Bootstwatch theme for use in the example apps.
3+
4+
# NOTE: This script requires the `purgecss` package to be installed.
5+
# You can install it with `npm install -g purgecss`.
6+
7+
import shutil
8+
import subprocess
9+
from pathlib import Path
10+
11+
import shinyswatch
12+
13+
from shiny import ui
14+
15+
app_ui = ui.page_sidebar(
16+
ui.sidebar(
17+
ui.input_numeric("n", "N", min=0, max=100, value=20),
18+
title="Parameters",
19+
),
20+
ui.h2("Output"),
21+
ui.output_text_verbatim("txt"),
22+
ui.markdown(
23+
"""
24+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
25+
26+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
27+
"""
28+
),
29+
shinyswatch.theme.minty,
30+
title="Theme Example",
31+
)
32+
33+
# If __file__ is not defined, use the current working directory
34+
if not globals().get("__file__"):
35+
__file__ = Path.cwd() / "_purgecss.py"
36+
37+
save_dir = Path(__file__).parent / "output"
38+
if save_dir.exists():
39+
shutil.rmtree(save_dir)
40+
save_dir.mkdir()
41+
app_ui.save_html(save_dir / "index.html", include_version=False)
42+
43+
purged_dir = Path(__file__).parent / "css"
44+
if purged_dir.exists():
45+
shutil.rmtree(purged_dir)
46+
purged_dir.mkdir(exist_ok=True)
47+
48+
args = [
49+
"purgecss",
50+
"--css",
51+
"output/lib/shinyswatch-css/bootswatch.min.css",
52+
"--content",
53+
"output/index.html",
54+
"--output",
55+
"css",
56+
]
57+
58+
subprocess.run(args)
59+
60+
(purged_dir / "bootswatch.min.css").rename(purged_dir / "bootswatch-minty.min.css")
61+
62+
if True:
63+
shutil.rmtree(save_dir)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from shiny import App, render, ui
2+
3+
app_ui = ui.page_sidebar(
4+
ui.sidebar(
5+
ui.input_numeric("n", "N", min=0, max=100, value=20),
6+
title="Parameters",
7+
),
8+
ui.h2("Output"),
9+
ui.output_text_verbatim("txt"),
10+
ui.markdown(
11+
"""
12+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
13+
14+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
15+
"""
16+
),
17+
title="Theme Example",
18+
theme="https://cdn.jsdelivr.net/npm/bootswatch@5.3.3/dist/sketchy/bootstrap.min.css",
19+
)
20+
21+
22+
def server(input, output, session):
23+
@render.text
24+
def txt():
25+
return f"n*2 is {input.n() * 2}"
26+
27+
28+
app = App(app_ui, server)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import shinyswatch
2+
3+
from shiny import App, render, ui
4+
5+
app_ui = ui.page_sidebar(
6+
ui.sidebar(
7+
ui.input_numeric("n", "N", min=0, max=100, value=20),
8+
title="Parameters",
9+
),
10+
ui.h2("Output"),
11+
ui.output_text_verbatim("txt"),
12+
ui.markdown(
13+
"""
14+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
15+
16+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
17+
"""
18+
),
19+
title="Theme Example",
20+
theme=shinyswatch.theme.slate(),
21+
)
22+
23+
24+
def server(input, output, session):
25+
@render.text
26+
def txt():
27+
return f"n*2 is {input.n() * 2}"
28+
29+
30+
app = App(app_ui, server)

shiny/api-examples/theme/app-core.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from pathlib import Path
2+
3+
from shiny import App, render, ui
4+
5+
app_ui = ui.page_sidebar(
6+
ui.sidebar(
7+
ui.input_numeric("n", "N", min=0, max=100, value=20),
8+
title="Parameters",
9+
),
10+
ui.h2("Output"),
11+
ui.output_text_verbatim("txt"),
12+
ui.markdown(
13+
"""
14+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
15+
16+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
17+
"""
18+
),
19+
title="Theme Example",
20+
# theme=shinyswatch.theme.slate,
21+
# theme="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css",
22+
theme=Path(__file__).parent / "css" / "bootswatch-minty.min.css",
23+
)
24+
25+
26+
def server(input, output, session):
27+
@render.text
28+
def txt():
29+
return f"n*2 is {input.n() * 2}"
30+
31+
32+
app = App(app_ui, server)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from shiny.express import input, render, ui
2+
3+
ui.page_opts(
4+
title="Theme Example",
5+
theme="https://cdn.jsdelivr.net/npm/bootswatch@5.3.3/dist/sketchy/bootstrap.min.css",
6+
)
7+
8+
with ui.sidebar(title="Parameters"):
9+
ui.input_numeric("n", "N", min=0, max=100, value=20)
10+
11+
ui.h2("Output")
12+
13+
14+
@render.code
15+
def txt():
16+
return f"n*2 is {input.n() * 2}"
17+
18+
19+
ui.markdown(
20+
"""
21+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
22+
23+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
24+
"""
25+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import shinyswatch
2+
3+
from shiny.express import input, render, ui
4+
5+
ui.page_opts(
6+
title="Theme Example",
7+
theme=shinyswatch.theme.slate,
8+
)
9+
10+
with ui.sidebar(title="Parameters"):
11+
ui.input_numeric("n", "N", min=0, max=100, value=20)
12+
13+
ui.h2("Output")
14+
15+
16+
@render.code
17+
def txt():
18+
return f"n*2 is {input.n() * 2}"
19+
20+
21+
ui.markdown(
22+
"""
23+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
24+
25+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
26+
"""
27+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pathlib import Path
2+
3+
from shiny.express import input, render, ui
4+
5+
ui.page_opts(
6+
title="Theme Example",
7+
theme=Path(__file__).parent / "css" / "bootswatch-minty.min.css",
8+
)
9+
10+
with ui.sidebar(title="Parameters"):
11+
ui.input_numeric("n", "N", min=0, max=100, value=20)
12+
13+
ui.h2("Output")
14+
15+
16+
@render.code
17+
def txt():
18+
return f"n*2 is {input.n() * 2}"
19+
20+
21+
ui.markdown(
22+
"""
23+
**AI-generated filler text.** In the world of exotic fruits, the durian stands out with its spiky exterior and strong odor. Despite its divisive smell, many people are drawn to its rich, creamy texture and unique flavor profile. This tropical fruit is often referred to as the "king of fruits" in various Southeast Asian countries.
24+
25+
Durians are known for their large size and thorn-covered husk, which requires careful handling. The flesh inside can vary in color from pale yellow to deep orange, with a custard-like consistency that melts in your mouth. Some describe its taste as a mix of sweet, savory, and creamy, while others find it overpowering and pungent.
26+
"""
27+
)

0 commit comments

Comments
 (0)