Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: traceback filtering for Output widgets #759

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def get_data_files():
'pytest',
'pytest-tornasync',
'matplotlib',
'ipywidgets'
'ipywidgets',
'pyppeteer',
]
},
'url': 'https://github.com/voila-dashboards/voila',
Expand Down
22 changes: 22 additions & 0 deletions share/jupyter/voila/templates/base/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

window.VoilaDebug = VoilaDebug = {
_widgetManagerPromiseResolve: null,
_widgetManagerPromise: null,
async waitForWidgetManager() {
await _widgetManagerPromise;
},
async waitForViews(modelId) {
const widgetManager = await window.VoilaDebug._widgetManagerPromise;
const model = await widgetManager._models[modelId];
await Promise.all(Object.values(model.views))
},
async waitForAllViews() {
const widgetManager = await window.VoilaDebug._widgetManagerPromise;
for(const modelId in widgetManager._models) {
await window.VoilaDebug.waitForViews(modelId);
}
}
}

window.VoilaDebug._widgetManagerPromise = new Promise((resolve) => window.VoilaDebug._widgetManagerPromiseResolve = resolve),

// NOTE: this file is not transpiled, async/await is the only modern feature we use here
require([window.voila_js_url || 'static/voila'], function(voila) {
// requirejs doesn't like to be passed an async function, so create one inside
Expand Down Expand Up @@ -45,6 +66,7 @@ require([window.voila_js_url || 'static/voila'], function(voila) {
});
await widgetManager.build_widgets();
voila.renderMathJax();
window.VoilaDebug._widgetManagerPromiseResolve(widgetManager);
}

if (document.readyState === 'complete') {
Expand Down
3 changes: 2 additions & 1 deletion share/jupyter/voila/templates/base/voila_setup.macro.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
<script>
requirejs.config({ baseUrl: '{{base_url}}voila/', waitSeconds: 30})
window.voila_js_url = "{{ static_url('voila.js')}}"
window.voila_main_url = "{{ static_url('main.js') }}"
requirejs(
[
"{{ static_url('main.js') }}",
voila_main_url,
{% for ext in nbextensions -%}
"{{base_url}}voila/nbextensions/{{ ext }}.js",
{% endfor %}
Expand Down
47 changes: 47 additions & 0 deletions tests/app/show_traceback_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import asyncio
import os

import pytest
import pyppeteer


@pytest.fixture(params=[True, False])
Expand All @@ -21,3 +25,46 @@ async def test_syntax_error(http_server_client, syntax_error_notebook_url, show_
else:
assert 'There was an error when executing cell' in output
assert 'This should not be executed' not in output


async def test_output_widget_traceback(http_server_client, exception_runtime_notebook_url, show_tracebacks):
options = dict(headless=False, devtools=True) if os.environ.get('VOILA_TEST_DEBUG_VISUAL', False) else {}
# with headless and gpu enabled, we get slightly different results on the same OS
# we can enable it if we need to, since we allow for a tolerance
launcher = pyppeteer.launcher.Launcher(options=options, autoClose=False, args=['--font-render-hinting=none', '--disable-gpu'])
browser = await launcher.launch()
page = await browser.newPage()
try:
await page.goto(http_server_client.get_url(exception_runtime_notebook_url), waitUntil='networkidle2')
await page.evaluate('async () => await (new Promise((resolve) => requirejs([voila_main_url], resolve)))')
await page.evaluate('async () => await window.VoilaDebug.widgetManagerPromise')
await page.evaluate('async () => await window.VoilaDebug.waitForAllViews()')
await page.evaluate("document.querySelector('.button2').click()")
for i in range(20):
await asyncio.sleep(0.05)
output_text = await page.evaluate("document.querySelector('.output_exception').innerText")
if output_text:
break
else:
assert False, f"Never received output"
if show_tracebacks:
assert 'this happens after the notebook is executed' in output_text
else:
assert 'this happens after the notebook is executed' not in output_text
except Exception as e: # noqa
if os.environ.get('VOILA_TEST_DEBUG_VISUAL', False):
import traceback
traceback.print_exc()
# may want to add --async-test-timeout=60 to pytest arguments
print("Waiting for 60 second for visual inspection (hit ctrl-c to break)")
import sys
sys.stdout.flush()
sys.stderr.flush()
try:
await asyncio.sleep(60)
except: # noqa ignore ctrl-c
pass
raise e
finally:
await browser.close()
await launcher.killChrome()
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def syntax_error_notebook_url(base_url):
return base_url + "voila/render/syntax_error.ipynb"


@pytest.fixture
def exception_runtime_notebook_url(base_url):
return base_url + "voila/render/exception_runtime.ipynb"


@pytest.fixture
def voila_notebook(notebook_directory):
return os.path.join(notebook_directory, 'print.ipynb')


# this forces the event_loop fixture in pytest_asyncio to use the same ioloop as pytest_tornasync
@pytest.fixture()
def event_loop(io_loop):
import asyncio
return asyncio.get_event_loop()
2 changes: 2 additions & 0 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ def start(self):
parent=self,
connection_dir=self.connection_dir,
kernel_spec_manager=self.kernel_spec_manager,
allow_tracebacks=self.voila_configuration.show_tracebacks,
traceback_replacement_message='An error occurred, run Voilà with --show_traceback=True or --debug to show the traceback.',
allowed_message_types=[
'comm_open',
'comm_close',
Expand Down