Skip to content

Commit 0a347dd

Browse files
committed
add styling utils
1 parent 60d54e0 commit 0a347dd

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

docs/examples/python/__init__.py

Whitespace-only changes.

docs/examples/python/quick_start/__init__.py

Whitespace-only changes.

docs/examples/python/quick_start/displaying_data.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,10 @@ def profile():
2828
# end
2929
if __name__ == "__main__":
3030
from reactpy import run
31+
from reactpy.utils import _read_docs_css
3132

32-
run(profile)
33+
@component
34+
def styled_app():
35+
return html._(html.style(_read_docs_css()), profile())
36+
37+
run(styled_app)

docs/examples/python/quick_start/sharing_data_between_components.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ def my_button(count, on_click):
2323
# end
2424
if __name__ == "__main__":
2525
from reactpy import run
26+
from reactpy.utils import _read_docs_css
2627

27-
run(my_app)
28+
@component
29+
def styled_app():
30+
return html._(html.style(_read_docs_css()), my_app())
31+
32+
run(styled_app)

docs/examples/python/quick_start/updating_the_screen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ def handle_click(event):
2323
# end
2424
if __name__ == "__main__":
2525
from reactpy import run
26+
from reactpy.utils import _read_docs_css
2627

27-
run(my_app)
28+
@component
29+
def styled_app():
30+
return html._(html.style(_read_docs_css()), my_app())
31+
32+
run(styled_app)

docs/examples/python/tutorial_tic_tac_toe/tic_tac_toe.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,10 @@ def calculate_winner(squares):
117117
# end
118118
if __name__ == "__main__":
119119
from reactpy import run
120+
from reactpy.utils import _read_docs_css
120121

121-
run(game)
122+
@component
123+
def styled_app():
124+
return html._(html.style(_read_docs_css()), game())
125+
126+
run(styled_app)

src/py/reactpy/reactpy/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import inspect
4+
import os
35
import re
46
from collections.abc import Iterable
57
from itertools import chain
@@ -305,3 +307,16 @@ def _vdom_attr_to_html_str(key: str, value: Any) -> tuple[str, str]:
305307

306308
# Pattern for delimitting camelCase names (e.g. camelCase to camel-case)
307309
_CAMEL_CASE_SUB_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
310+
311+
312+
def _read_docs_css():
313+
"""Reads a CSS file in the docs with the same relative path."""
314+
py_path = os.path.abspath((inspect.stack()[1])[1])
315+
css_path = (
316+
py_path.replace("/python/", "/css/")
317+
.replace("\\python\\", "\\css\\")
318+
.replace(".py", ".css")
319+
)
320+
321+
with open(css_path, encoding="UTF-8") as css_file:
322+
return css_file.read()

0 commit comments

Comments
 (0)