Skip to content

Commit b5efe2f

Browse files
committed
Parse the body and change the pyconfig src to point at correct TOML file.
1 parent 3469080 commit b5efe2f

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
## Eventually
1313

14-
- Get nox to work with downloaded pyodide/pyscript
14+
- Get numpy, pandas, etc. downloaded into local dir
1515
- Get rid of Poetry
1616

1717
## Done

docs/developers/cdn_mode.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ We'll just look for the `src/pyscript` and `src/pyodide` directory.
8282
If they exist, then someone downloaded the assets.
8383
That's a good flag for whether to point at those directories.
8484

85+
What is the action to take?
86+
The `py_config.local.toml` file has a `runtimes.src` entry pointing to the local `pyodide.js`.
87+
The `py_config.cdn.toml` file points at the CDN version.
88+
8589
## Actions
8690

8791
- Remove the timeout
8892
- Test/implementation that calls a `is_local` function to detect the correct mode
89-
- Test/implementation which wires that into the HTML munger
90-
- Make a `pyscript_path` which is passed into the `example.jinja2` template
91-
- E2E test which detects correct case
92-
- Revisit if `fake` and interceptor are needed
93+
- Test/implementation which wires that into the HTML munger
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
autoclose_loader = true
2+
3+
[[runtimes]]
4+
src = "https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js"

src/psc/resources.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ def is_local(test_path: Path | None = PYODIDE) -> bool:
5151
return test_path.exists()
5252

5353

54-
def get_body_content(s: BeautifulSoup) -> str:
54+
def get_body_content(s: BeautifulSoup, test_path: Path | None = PYODIDE) -> str:
5555
"""Get the body node but raise an exception if not present."""
56+
57+
# Choose the correct TOML file for local vs remote.
58+
toml_name = "local" if is_local(test_path) else "cdn"
59+
src = f"../py_config.{toml_name}.toml"
60+
61+
# Get the body and patch the py_config src
5662
body_element = s.select_one("body")
63+
py_config = body_element.find("py-config")
64+
py_config["src"] = src
5765
return f"{body_element.decode_contents()}"
5866

5967

tests/test_generic_example.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,3 @@ def test_hello_world_js(test_client: TestClient) -> None:
3838
"""Test the static assets for Hello World."""
3939
response = test_client.get("/gallery/examples/hello_world/hello_world.js")
4040
assert response.status_code == 200
41-
42-
43-
#
44-
#
45-
# @pytest.mark.full
46-
# def test_hello_world_full(fake_page: Page) -> None:
47-
# """Use Playwright to do a test on Hello World."""
48-
# # Use `PWDEBUG=1` to run "head-ful" in Playwright test app
49-
# url = "http://fake/gallery/examples/hello_world/index.html"
50-
# fake_page.goto(url)
51-
# title = fake_page.title()
52-
# assert title == "Hello World Python"

tests/test_resources.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,34 @@ def test_get_no_head_nodes() -> None:
5454

5555

5656
def test_get_main() -> None:
57-
"""Return the main node from an example."""
58-
example_html = '<body><main class="content">Hello <em>world</em></main></body>'
57+
"""Return the body nodes from an example."""
58+
example_html = '<body><py-config src="../x.toml">abc</py-config></body>'
5959
soup = BeautifulSoup(example_html, "html5lib")
6060
body = get_body_content(soup)
61-
assert body == '<main class="content">Hello <em>world</em></main>'
61+
assert body == '<py-config src="../py_config.local.toml">abc</py-config>'
62+
63+
64+
def test_get_py_config_local() -> None:
65+
"""Return the main node and test setting py-config src."""
66+
example_html = '<body><py-config src="../x.toml">abc</py-config></body>'
67+
soup = BeautifulSoup(example_html, "html5lib")
68+
body = get_body_content(soup)
69+
body_soup = BeautifulSoup(body, "html5lib")
70+
py_config = body_soup.find("py-config")
71+
actual = py_config["src"]
72+
assert "../py_config.local.toml" == actual
73+
74+
75+
def test_get_py_config_cdn() -> None:
76+
"""Return the main node and test setting py-config src."""
77+
example_html = '<body><py-config src="../x.toml">abc</py-config></body>'
78+
soup = BeautifulSoup(example_html, "html5lib")
79+
test_path = Path("/x")
80+
body = get_body_content(soup, test_path=test_path)
81+
body_soup = BeautifulSoup(body, "html5lib")
82+
py_config = body_soup.find("py-config")
83+
actual = py_config["src"]
84+
assert "../py_config.cdn.toml" == actual
6285

6386

6487
def test_example_bad_path() -> None:
@@ -136,6 +159,6 @@ def test_is_local_no_path() -> None:
136159

137160
def test_is_local_broken_path() -> None:
138161
"""Test the local case where a directory will not exist."""
139-
test_path = Path("/xxxxxx")
162+
test_path = Path("/xxx")
140163
actual = is_local(test_path)
141164
assert not actual

0 commit comments

Comments
 (0)