Skip to content

Commit

Permalink
Merge pull request #60 from m-rossi/javascript-visualizations
Browse files Browse the repository at this point in the history
Add support for JavaScript-based visualizations
  • Loading branch information
m-rossi authored May 14, 2021
2 parents c341763 + 6577ae7 commit 43826ad
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 14 deletions.
3 changes: 3 additions & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ requirements:
- setuptools >=38.6.0
- setuptools-scm
run:
- importlib_resources # [py<39]
- lxml
- nbconvert >=5.5
- notebook >=5.0
Expand All @@ -42,9 +43,11 @@ test:
- mock
- nbformat
- pillow >=6.0.0
- plotly
- pytest
- pytest-cov
- pytest-lazy-fixture
- python-kaleido
- sympy
commands:
- pytest --pyargs jupyter_docx_bundler
Expand Down
31 changes: 28 additions & 3 deletions jupyter_docx_bundler/converters.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import base64
try:
from importlib.resources import files as resources_files
except ImportError:
from importlib_resources import files as resources_files
import json
import os
from pathlib import Path
import re
import tempfile
from pathlib import Path

from nbconvert import preprocessors
import nbformat
import pandas as pd
import pypandoc
import requests

from nbconvert import preprocessors

RE_IMAGE = re.compile(r'!\[.+]\((?!attachment:).+\)')
RE_EXTRA_TITLE = re.compile(r'\s".+"')
Expand Down Expand Up @@ -187,6 +191,27 @@ def preprocess(content, path, handler=None):
handler.log.warning(f'Conversion of pandas HTML-table failed : {e}')
else:
raise e
# plotly figure
elif 'data' in output and 'application/vnd.plotly.v1+json' in output['data']:
try:
from plotly import io
from kaleido.scopes.plotly import PlotlyScope

scope = PlotlyScope(
plotlyjs=resources_files('plotly') / 'package_data' / 'plotly.min.js',
)

fig = io.from_json(
json.dumps(output['data']['application/vnd.plotly.v1+json'])
)
imagedata = scope.transform(fig, format='png', scale=2.0)
output['data']['image/png'] = base64.b64encode(imagedata)
except ModuleNotFoundError as e:
if handler is not None:
handler.log.warning('Found plotly-figure in notebook, we need plotly '
'and kaleido to convert figure.')
else:
raise e
# latex but not code cells (it write also a latex output)
elif 'data' in output and 'text/latex' in output['data'] and \
'text/html' not in output['data']:
Expand Down
51 changes: 51 additions & 0 deletions jupyter_docx_bundler/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,55 @@ def ipython_output_notebook(tmpdir, request):
return nb


@pytest.fixture(params=[10])
def plotly_notebook(tmpdir, request):
nb = nbformat.v4.new_notebook()
image_count = 0

# imports
nb.cells.append(
nbformat.v4.new_markdown_cell(
'# Imports',
)
)
nb.cells.append(
nbformat.v4.new_code_cell(
'\n'.join([
'import numpy as np',
'import plotly.express as px',
])
)
)

# single plotly image per cell
nb.cells.append(
nbformat.v4.new_markdown_cell(
'# single plotly image per cell',
)
)
for _ in range(request.param):
nb.cells.append(
nbformat.v4.new_code_cell(
'\n'.join([
'fig = px.line(x=np.arange(100), y=np.random.randn(100))',
'fig.show()',
])
)
)
image_count += request.param

# update metadata
nb['metadata'].update({
'path': f'{tmpdir}',
'image_count': image_count,
})

ep = ExecutePreprocessor()
ep.preprocess(nb, {'metadata': {'path': tmpdir}})

return nb


@pytest.fixture(
params=[
lazy_fixture('math_with_space_notebook'),
Expand Down Expand Up @@ -641,10 +690,12 @@ def test_notebook(request):
params=[
lazy_fixture('matplotlib_notebook'),
lazy_fixture('markdown_images_notebook'),
lazy_fixture('plotly_notebook'),
],
ids=[
'matplotlib',
'images',
'plotly',
]
)
def images_notebook(request):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
importlib_resources
lxml
nbconvert >= 5.5
notebook >= 5.0
Expand Down
2 changes: 2 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mock
nbformat
numpy
pillow >= 6.0.0
plotly
pytest
pytest-cov
pytest-lazy-fixture
python-kaleido
sympy
30 changes: 19 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import sys

from setuptools import setup


with open('README.md') as f:
long_description = f.read()

install_requires = [
'lxml',
'nbconvert>=5.5',
'notebook>=5.0',
'pandas',
'pandocfilters',
'pypandoc>=1.4',
'requests',
'tabulate',
'tornado',
]
if sys.version_info.major <= 3 and sys.version_info.minor < 9:
install_requires += ['importlib_metadata']

setup(
author='Marco Rossi',
author_email='[email protected]',
Expand All @@ -20,28 +36,20 @@
extras_require={
'test': [
'ipython>=7.0'
'kaleido',
'matplotlib>=3.1',
'mock',
'nbformat',
'numpy',
'pillow>=6.0.0',
'plotly',
'pytest',
'pytest-cov',
'pytest-lazy-fixture',
'sympy',
],
},
install_requires=[
'lxml',
'nbconvert>=5.5',
'notebook>=5.0',
'pandas',
'pandocfilters',
'pypandoc>=1.4',
'requests',
'tabulate',
'tornado',
],
install_requires=install_requires,
keywords=[
'jupyter',
'docx',
Expand Down

0 comments on commit 43826ad

Please sign in to comment.