Skip to content

Commit

Permalink
Fixup use with a running event loop (#1420)
Browse files Browse the repository at this point in the history
* Fixup use with a running event loop

* Python 3.6 support
  • Loading branch information
SylvainCorlay authored Sep 23, 2020
1 parent 7a0d5ea commit 98a3805
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions nbconvert/exporters/webpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import asyncio

from traitlets import Bool
import concurrent.futures

from .html import HTMLExporter


class WebPDFExporter(HTMLExporter):
"""Writer designed to write to PDF files.
Expand Down Expand Up @@ -36,7 +39,11 @@ def run_pyppeteer(self, html):
"""Run pyppeteer."""

async def main():
browser = await self._check_launch_reqs()()
browser = await self._check_launch_reqs()(
handleSIGINT=False,
handleSIGTERM=False,
handleSIGHUP=False,
)
page = await browser.newPage()
await page.waitFor(100)
await page.goto('data:text/html,'+html, waitUntil='networkidle0')
Expand Down Expand Up @@ -66,7 +73,14 @@ async def main():
await browser.close()
return pdf_data

pdf_data = asyncio.get_event_loop().run_until_complete(main())
pool = concurrent.futures.ThreadPoolExecutor()
# TODO: when dropping Python 3.6, use
# pdf_data = pool.submit(asyncio.run, main()).result()
def run_coroutine(coro):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coro)
pdf_data = pool.submit(run_coroutine, main()).result()
return pdf_data

def from_notebook_node(self, nb, resources=None, **kw):
Expand Down

0 comments on commit 98a3805

Please sign in to comment.