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

Set preserveDrawingBuffer in webgl and add example to freeze notebooks #52

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
105 changes: 105 additions & 0 deletions examples/Freezing vispy visualizations.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"This notebook shows how to embed a static views of interactive vispy scenes into notebooks after using them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('../test')\n",
"import test_scenes\n",
"\n",
"import plato, plato.draw.vispy as draw\n",
"\n",
"test_scenes.colored_spheres().convert(draw).show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%javascript\n",
"// This snippet automatically exports all vispy canvases in a notebook into a\n",
"// static form that can be exported to HTML, for example\n",
"var cells = Jupyter.notebook.get_cells()\n",
"\n",
"for(var cellix = 0; cellix < cells.length; ++cellix)\n",
"{\n",
" var cell = cells[cellix]\n",
"\n",
" var new_nodes = []\n",
" var output_area = cell.output_area\n",
"\n",
" if(typeof output_area !== 'undefined')\n",
" {\n",
" var canvases = output_area.element[0].querySelectorAll('canvas')\n",
" for(var i = 0; i < canvases.length; ++i)\n",
" {\n",
" var new_node = document.createElement('img')\n",
" new_node.src = canvases[i].toDataURL('image/jpeg')\n",
" canvases[i].parentElement.replaceChild(new_node, canvases[i])\n",
" new_nodes.push(new_node)\n",
" }\n",
"\n",
" for(var outix = 0; outix < output_area.outputs.length; ++outix)\n",
" {\n",
" var output = output_area.outputs[outix]\n",
"\n",
" if(output.output_type == 'display_data' &&\n",
" typeof output.data !== 'undefined' &&\n",
" output.data['text/plain'].startsWith('VispyWidget'))\n",
" {\n",
" var contents = new_nodes.pop(0).outerHTML\n",
" output.data = {\n",
" 'text/plain': '<embedded image>',\n",
" 'text/html': contents}\n",
" }\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
5 changes: 5 additions & 0 deletions plato/draw/vispy/Canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ def __init__(self, scene, **kwargs):
self._selection_callback = None
self._clip_planes = np.array([1, 100], dtype=np.float32)

if self._webgl:
backend_kwargs = kwargs.setdefault('backend_kwargs', {})
webgl_kwargs = backend_kwargs.setdefault('webgl', {})
webgl_kwargs['preserveDrawingBuffer'] = True

super(Canvas, self).__init__(size=scene.size_pixels.astype(np.uint32), **kwargs)

gloo.set_viewport(0, 0, *scene.size_pixels.astype(np.uint32))
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
],
description='Geometry and visualization tools for collections of particles',
extras_require={
'pyside': ['pyside', 'vispy >= 0.5.3'],
'pyside2': ['pyside2', 'vispy >= 0.6'],
'pyside': ['pyside', 'vispy >= 0.7'],
'pyside2': ['pyside2', 'vispy >= 0.7'],
'pythreejs': ['pythreejs'],
},
install_requires=['numpy', 'scipy', 'rowan'],
Expand Down