Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(Theme): Fix example and clarify usage #1491

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions shiny/ui/_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,47 @@ class Theme:
caches the compiled CSS so that it's only compiled for the first user to load your
app, but you can speed up app loading (and avoid the runtime `libsass` dependency)
by pre-compiling the theme CSS and saving it to a file. To do this, use the
`.to_css()` method to render the theme to a single minified CSS string. Once saved
to a file, the CSS can be used in any Shiny app by passing the file path to the
`theme` argument instead of the `Theme` object.
`.to_css()` method to render the theme to a single minified CSS string.

```{.python filename="my_theme.py"}
from pathlib import Path

from shiny import ui

my_theme = (
ui.Theme("shiny")
.add_defaults(
my_purple="#aa00aa",
)
.add_mixins(
headings_color="$success",
bar_color="$purple",
select_color_text="$orange",
headings_color="$my-purple",
)
)

with open("my_theme.css", "w") as f:
with open(Path(__file__).parent / "my_theme.css", "w") as f:
f.write(my_theme.to_css())
```

Run this script with `python my_theme.py` to generate the CSS file. Once saved to a
file, the CSS can be used in any Shiny app by passing the file path to the `theme`
argument instead of the `Theme` object.

```{.python filename="app.py"}
from shiny import ui
from pathlib import Path

ui = ui.page_fluid(
from shiny import App, ui

app_ui = ui.page_fluid(
ui.h2("Hello, themed Shiny!"),
# App content here
title="My App",
theme="my_theme.css",
theme=Path(__file__).parent / "my_theme.css",
)

def server(input):
pass

app = App(app_ui, server)
```

Parameters
Expand Down
Loading