Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
holoviz
GitHub Repository: holoviz/panel
Path: blob/main/scripts/panelite/test/test_panelite.py
2012 views
1
"""
2
Before running this script make sure you have
3
4
```bash
5
git pull
6
pip install -e . -U
7
python setup.py develop
8
```
9
10
Then run the script to create/ update the notebooks for Panelite
11
12
```bash
13
python scripts/generate_panelite_content.py
14
```
15
16
Follow the steps in the Panel Jupyterlite guide including
17
18
```bash
19
cd lite
20
pip install -r requirements.txt -U
21
jupyter lite build --output-dir ./dist
22
```
23
24
Start the http server
25
26
```bash
27
cd dist
28
python -m http.server
29
```
30
31
Reset your files as described in https://github.com/jupyterlite/jupyterlite/issues/9#issuecomment-875870620
32
33
Run the tests
34
35
```bash
36
pytest scripts/test_panelite/test_panelite.py --headed
37
```
38
"""
39
import pathlib
40
41
import pytest
42
43
from playwright.sync_api import Page, expect
44
45
from scripts.test_panelite.notebooks_with_panelite_issues import (
46
NOTEBOOK_ISSUES,
47
)
48
from scripts.test_panelite.utils import Retrier
49
50
URL = "https://panelite.holoviz.org"
51
URL = "http://localhost:8000"
52
53
PANEL_BASE = pathlib.Path(__file__).parent.parent.parent
54
FILES = PANEL_BASE / 'lite' / 'files'
55
56
def get_panelite_nb_paths():
57
nbs = list(FILES.glob('*/*/*.ipynb')) + list(FILES.glob('*/*.*'))
58
for nb in nbs:
59
path = str(nb).replace("\\", "/").split("files/")[-1]
60
if path.endswith(".ipynb") and ".ipynb_checkpoints" not in path:
61
yield path
62
PATHS_WITH_NOTHING_TO_TEST = [
63
"gallery/demos/attractors.ipynb",
64
"gallery/demos/gapminders.ipynb",
65
"gallery/demos/glaciers.ipynb",
66
"gallery/demos/nyc_taxi.ipynb",
67
"gallery/demos/portfolio-optimizer.ipynb",
68
]
69
PATHS = list(path for path in get_panelite_nb_paths() if path not in PATHS_WITH_NOTHING_TO_TEST)
70
PATHS_WITHOUT_ISSUES = list(path for path in PATHS if path not in NOTEBOOK_ISSUES)
71
PATHS_WITH_ISSUES = list(path for path in PATHS if path in NOTEBOOK_ISSUES)
72
73
def _wait_for_notebook_tab(page, notebook):
74
tabs = page.get_by_role("tablist")
75
tabs.get_by_text(notebook)
76
77
def _select_kernel(page):
78
page.get_by_role("button", name="Python (Pyodide)").click()
79
page.get_by_role("button", name="Select").filter(has_text="Select").click()
80
81
def _run_all_cells(page):
82
page.get_by_role("menuitem", name="Run").get_by_text("Run").click()
83
page.get_by_text("Run All Cells").nth(2).click()
84
85
class RunCells(Exception):
86
"""Raised if an exception is raised while running all cells"""
87
88
def run_notebook(path, page):
89
"""Opens the notebook and runs all cells"""
90
url =URL + f"/lab/index.html?path={path}"
91
notebook = path.split("/")[-1]
92
93
retrier = Retrier(retries=3, delay=1)
94
while not retrier.accomplished:
95
with retrier:
96
page.goto(url)
97
_wait_for_notebook_tab(page, notebook)
98
_select_kernel(page)
99
_run_all_cells(page)
100
101
# To wait for * makes tests fail faster
102
# The data-status attribute cannot be expected to change within the default timeout
103
busy_cells = page.get_by_text("[*]:")
104
expect(busy_cells).not_to_have_count(0)
105
106
execution_indicator = page.locator('.jp-Notebook-ExecutionIndicator')
107
expect(execution_indicator).to_have_attribute("data-status", "unknown", timeout=10000)
108
expect(execution_indicator).to_have_attribute("data-status", "busy", timeout=10000)
109
expect(execution_indicator).to_have_attribute("data-status", "idle", timeout=45000)
110
111
112
@pytest.mark.parametrize(["path"], [(path,) for path in PATHS_WITHOUT_ISSUES])
113
def test_notebooks_without_issues(path, page: Page):
114
run_notebook(path, page)
115
cells_with_err = page.locator('div[data-mime-type="application/vnd.jupyter.stderr"]')
116
if cells_with_err.count()>0:
117
raise RunCells(cells_with_err.first.inner_text())
118
119
@pytest.mark.xfail
120
@pytest.mark.parametrize(["path"], [(path,) for path in PATHS_WITH_ISSUES])
121
def test_notebooks_with_issues(path, page: Page):
122
run_notebook(path, page)
123
cells_with_err = page.locator('div[data-mime-type="application/vnd.jupyter.stderr"]')
124
if cells_with_err.count()>0:
125
raise RunCells(cells_with_err.first.inner_text())
126
127