|
3 | 3 | import pandas as pd |
4 | 4 | import pytest |
5 | 5 |
|
| 6 | +from pygwalker.api import adapter, html, jupyter |
6 | 7 | from pygwalker.api import pygwalker as pygwalker_module |
7 | 8 | from pygwalker.api.pygwalker import PygWalker |
8 | 9 | from pygwalker.communications.base import BaseCommunication |
@@ -218,3 +219,92 @@ def test_pygwalker_export_dataframe_callback_stores_last_dataframe(monkeypatch): |
218 | 219 | ) |
219 | 220 | finally: |
220 | 221 | GlobalVarManager.last_exported_dataframe = previous_exported_dataframe |
| 222 | + |
| 223 | + |
| 224 | +@pytest.mark.parametrize( |
| 225 | + ("kwargs", "expected_kernel_computation"), |
| 226 | + [ |
| 227 | + ({}, False), |
| 228 | + ({"kernel_computation": True}, True), |
| 229 | + ({"env": "JupyterConvert", "kernel_computation": True}, False), |
| 230 | + ], |
| 231 | +) |
| 232 | +def test_jupyter_walk_sets_pygwalker_kernel_computation_mode( |
| 233 | + monkeypatch, |
| 234 | + kwargs, |
| 235 | + expected_kernel_computation, |
| 236 | +): |
| 237 | + monkeypatch.setattr(pygwalker_module, "check_update", lambda: None) |
| 238 | + monkeypatch.setattr(pygwalker_module, "track_event", lambda *_args, **_kwargs: None) |
| 239 | + monkeypatch.setattr(jupyter, "check_kaggle", lambda: False) |
| 240 | + monkeypatch.setattr(jupyter, "check_convert", lambda: False) |
| 241 | + monkeypatch.setattr(jupyter, "get_kaggle_run_type", lambda: "") |
| 242 | + monkeypatch.setattr(PygWalker, "display_on_jupyter_use_widgets", lambda self: None) |
| 243 | + monkeypatch.setattr(PygWalker, "display_on_jupyter", lambda self: None) |
| 244 | + monkeypatch.setattr(PygWalker, "display_on_convert_html", lambda self: None) |
| 245 | + |
| 246 | + walker = jupyter.walk( |
| 247 | + pd.DataFrame([{"city": "London", "value": 1}]), |
| 248 | + gid="entry", |
| 249 | + **kwargs, |
| 250 | + ) |
| 251 | + |
| 252 | + assert walker.kernel_computation is expected_kernel_computation |
| 253 | + |
| 254 | + |
| 255 | +def test_to_html_returns_iframe_for_pygwalker_static_export(monkeypatch): |
| 256 | + monkeypatch.setattr(pygwalker_module, "check_update", lambda: None) |
| 257 | + monkeypatch.setattr(pygwalker_module, "track_event", lambda *_args, **_kwargs: None) |
| 258 | + monkeypatch.setattr(pygwalker_module, "get_local_user_id", lambda: "test-user") |
| 259 | + |
| 260 | + rendered = html.to_html( |
| 261 | + pd.DataFrame([{"city": "London", "value": 1}]), |
| 262 | + gid="static", |
| 263 | + appearance="light", |
| 264 | + width="640px", |
| 265 | + height="480px", |
| 266 | + ) |
| 267 | + |
| 268 | + assert 'id="gwalker-static"' in rendered |
| 269 | + assert 'width="640px"' in rendered |
| 270 | + assert 'height="480px"' in rendered |
| 271 | + assert "srcdoc=" in rendered |
| 272 | + |
| 273 | + |
| 274 | +@pytest.mark.parametrize( |
| 275 | + ("runtime_env", "expected_backend"), |
| 276 | + [ |
| 277 | + ("jupyter", "jupyter"), |
| 278 | + ("script", "webserver"), |
| 279 | + ], |
| 280 | +) |
| 281 | +def test_public_walk_routes_pygwalker_to_environment_backend( |
| 282 | + monkeypatch, |
| 283 | + runtime_env, |
| 284 | + expected_backend, |
| 285 | +): |
| 286 | + calls = [] |
| 287 | + |
| 288 | + def fake_jupyter_walk(*args, **kwargs): |
| 289 | + calls.append(("jupyter", args, kwargs)) |
| 290 | + return "jupyter-walker" |
| 291 | + |
| 292 | + def fake_webserver_walk(*args, **kwargs): |
| 293 | + calls.append(("webserver", args, kwargs)) |
| 294 | + return "webserver-walker" |
| 295 | + |
| 296 | + monkeypatch.setattr(adapter, "get_current_env", lambda: runtime_env) |
| 297 | + monkeypatch.setattr(adapter.jupyter, "walk", fake_jupyter_walk) |
| 298 | + monkeypatch.setattr(adapter.webserver, "walk", fake_webserver_walk) |
| 299 | + |
| 300 | + result = adapter.walk( |
| 301 | + pd.DataFrame([{"city": "London", "value": 1}]), |
| 302 | + gid="entry", |
| 303 | + kernel_computation=True, |
| 304 | + ) |
| 305 | + |
| 306 | + assert result == f"{expected_backend}-walker" |
| 307 | + assert [call[0] for call in calls] == [expected_backend] |
| 308 | + if expected_backend == "webserver": |
| 309 | + assert calls[0][2]["auto_open"] is True |
| 310 | + assert calls[0][2]["auto_shutdown"] is True |
0 commit comments