Skip to content

Commit ad51114

Browse files
authored
Improved component landing page (#687)
* Update code-example parser * Updated component overview page * Update component landing page language * Fix bug in R snippet * Add test
1 parent e9df5ec commit ad51114

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

docs/components_page/components/__tests__/test_snippets.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,38 @@ def test_jl_snippets(dash_thread_server, dashjl_server, config):
120120
)
121121

122122

123+
def test_landing_page_example(dash_thread_server, dashr_server, dashjl_server):
124+
index_dir = HERE.parent / "index"
125+
py_source = (index_dir / "simple.py").read_text()
126+
r_source = (
127+
(index_dir / "simple.R").read_text().replace("8050", str(R_PORT))
128+
)
129+
jl_source = (
130+
(index_dir / "simple.jl").read_text().replace("8050", str(JL_PORT))
131+
)
132+
133+
app = py_source_to_app(py_source, {})
134+
dash_thread_server.start(app, port=8050)
135+
py_layout = requests.get(f"{dash_thread_server.url}/_dash-layout").json()
136+
137+
dashr_server.start(r_source)
138+
r_layout = requests.get(f"{dashr_server.url}/_dash-layout").json()
139+
140+
dashjl_server.start(jl_source)
141+
jl_layout = requests.get(f"{dashjl_server.url}/_dash-layout").json()
142+
143+
# Test layouts match
144+
unittest.TestCase().assertDictEqual(
145+
drop_keys(py_layout), drop_keys(r_layout)
146+
)
147+
unittest.TestCase().assertDictEqual(
148+
drop_keys(py_layout), drop_keys(jl_layout)
149+
)
150+
151+
123152
def assert_layouts_equal(
124153
compare, runner, wrapper, port, py_runner, py_env, py_port
125154
):
126-
127155
# Get python snippet layout
128156
app = py_source_to_app(
129157
PY_WRAPPER.format(

docs/components_page/components/index.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ title: Components
33
lead: Usage examples for all components in dash-bootstrap-components
44
---
55

6-
The component documentation for _dash-bootstrap-components_ contains many snippets showing example usage, as well as api documentation for each component explaining the different props that you can set.
6+
The component documentation for _dash-bootstrap-components_ contains many snippets showing example usage, as well as API documentation for each component explaining the different props that you can set.
77

88
Example snippets for the different components will look something like this, with tabs to switch between R or Julia versions of the examples.
99

10-
{{example:components/index/simple.py:layout}}
10+
{{example:components/badge/simple.py:badge}}
1111

1212
## Adding the snippets to your app
1313

1414
Note that the contents of each snippet **do not by themselves constitute a working app**. We generally omit boilerplate code such as standard imports and instantiation of the app. In the above example you would additionally need to:
1515

16-
1. import Dash
17-
2. create a Dash app instance (making sure to link a Bootstrap stylesheet, see the [themes documentation](/docs/themes/) for details)
18-
3. assign `layout` to the app layout
19-
4. start the Dash server.
16+
1. Import Dash.
17+
2. Create a Dash app instance (making sure to link a Bootstrap stylesheet, see the [themes documentation](/docs/themes/) for details).
18+
3. Add the example to the app's layout.
19+
4. Start the Dash server.
2020

21-
If any of the above steps are unclear, please take a look at the [Quickstart](/docs/quickstart/) instructions for creating a basic app, the [examples](https://github.com/facultyai/dash-bootstrap-components/tree/main/examples) in our GitHub repository, or refer to the official Dash documentation for [Python](https://dash.plotly.com/), [R](https://dashr.plotly.com/), or [Julia](https://dash-julia.plotly.com/).
21+
For example, a simple application incorporating example above could look like this, where we wrap the snippet in a container and add it to the layout.
22+
23+
{{code-example:components/index/simple.py}}
24+
25+
There is more detail on this in the [Quickstart](/docs/quickstart/) instructions for creating a basic app. Additionally, the [examples](https://github.com/facultyai/dash-bootstrap-components/tree/main/examples) in our GitHub repository demonstrate how multiple components can be combined to create a feature rich application.
26+
27+
For more details on Dash in general, please refer to the official Dash documentation for [Python](https://dash.plotly.com/), [R](https://dashr.plotly.com/), or [Julia](https://dash-julia.plotly.com/).
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
# 1. Import Dash
2+
library(dash)
13
library(dashBootstrapComponents)
2-
library(dashHtmlComponents)
34

4-
layout <- dbcAlert(
5-
paste(
6-
"This is an example of a component being used in the wild.",
7-
"Below me, you can find the code used to create me."
5+
# 2. Create a Dash app instance
6+
app <- Dash$new(external_stylesheets = dbcThemes$BOOTSTRAP)
7+
8+
# 3. Add the example to the app's layout
9+
# First we copy the snippet from the docs
10+
badge <- dbcButton(
11+
list(
12+
"Notifications",
13+
dbcBadge("4", color = "light", text_color = "primary", className = "ms-1")
814
),
9-
color = "info"
15+
color = "primary"
1016
)
17+
18+
# Then we incorporate the snippet into our layout.
19+
# This example keeps it simple and just wraps it in a Container
20+
app$layout(dbcContainer(badge, fluid = TRUE))
21+
22+
# 5. Start the Dash server
23+
app$run_server(port = 8050)
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
using DashBootstrapComponents, DashHtmlComponents
1+
# 1. Import Dash
2+
using Dash, DashBootstrapComponents
23

3-
layout = dbc_alert(
4-
"This is an example of a component being used in the wild. " *
5-
"Below me, you can find the code used to create me.",
6-
color = "info",
4+
# 2. Create a Dash app instance
5+
app = dash(external_stylesheets = [dbc_themes.BOOTSTRAP])
6+
7+
# 3. Add the example to the app's layout
8+
# First we copy the snippet from the docs
9+
badge = dbc_button(
10+
[
11+
"Notifications",
12+
dbc_badge("4", color = "light", text_color = "primary", className = "ms-1"),
13+
],
14+
color = "primary",
715
)
16+
17+
# Then we incorporate the snippet into our layout.
18+
# This example keeps it simple and just wraps it in a Container
19+
app.layout = dbc_container(badge, fluid = true)
20+
21+
# 5. Start the Dash server
22+
run_server(app, "0.0.0.0", 8050)
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
# 1. Import Dash
2+
import dash
13
import dash_bootstrap_components as dbc
24

3-
layout = dbc.Alert(
4-
"This is an example of a component being used in the wild. "
5-
"Below me, you can find the code used to create me.",
6-
color="info",
5+
# 2. Create a Dash app instance
6+
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
7+
8+
# 3. Add the example to the app's layout
9+
# First we copy the snippet from the docs
10+
badge = dbc.Button(
11+
[
12+
"Notifications",
13+
dbc.Badge("4", color="light", text_color="primary", className="ms-1"),
14+
],
15+
color="primary",
716
)
17+
18+
# Then we incorporate the snippet into our layout.
19+
# This example keeps it simple and just wraps it in a Container
20+
app.layout = dbc.Container(badge, fluid=True)
21+
22+
# 5. Start the Dash server
23+
if __name__ == "__main__":
24+
app.run_server()

docs/components_page/components/layout.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can also control horizontal alignment of columns using the `justify` keyword
8282

8383
Sometimes you may wish to use Bootstrap's grid system for specifying the layout of your app, but you don't want the changes Bootstrap makes to the typography, or to load all the additional CSS classes that Bootstrap specifies. In such a situation, you can link only the CSS required for the grid system using the `themes` module.
8484

85-
{{code-example:components/layout/grid_only.py:python}}
85+
{{code-example:components/layout/grid_only.py}}
8686

8787
Alternatively download `bootstrap-grid.css` from the [Bootstrap website](https://getbootstrap.com/docs/4.2/getting-started/download/) and place it in your app's `assets/` directory. See the [Plotly Dash documentation](https://dash.plot.ly/external-resources) for details.
8888

docs/components_page/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dash import dcc, html
33

44

5-
def HighlightedSource(py_source, r_source, jl_source):
5+
def HighlightedSource(py_source, r_source, jl_source, className="px-3"):
66
return dbc.Tabs(
77
[
88
dbc.Tab(
@@ -17,7 +17,7 @@ def HighlightedSource(py_source, r_source, jl_source):
1717
]
1818
if source is not None
1919
],
20-
className="px-3",
20+
className=className,
2121
)
2222

2323

docs/components_page/markdown_parser.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import markdown
66
from dash import dcc, html
77

8-
from .helpers import ExampleContainer, load_source_with_environment
8+
from .helpers import (
9+
ExampleContainer,
10+
HighlightedSource,
11+
load_source_with_environment,
12+
)
913

1014
__all__ = ["parse"]
1115

@@ -91,12 +95,17 @@ def _safe_load_source(source_path, ext):
9195
return None
9296

9397

94-
def _parse_code_example(data):
95-
source_path, language = data.split(":")
96-
source = (HERE / source_path).read_text().strip()
98+
def _parse_code_example(filename):
99+
source_path = HERE / filename
100+
py_source = (HERE / source_path).read_text().strip()
101+
r_source = _safe_load_source(source_path, "R")
102+
jl_source = _safe_load_source(source_path, "jl")
103+
97104
return html.Div(
98-
dcc.Markdown(f"```{language}\n{source}\n```"),
99-
className="source-container",
105+
HighlightedSource(
106+
py_source, r_source, jl_source, className="pb-0 card-header"
107+
),
108+
className="border source-container rounded",
100109
)
101110

102111

0 commit comments

Comments
 (0)