Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jun 21, 2022
1 parent 8929da7 commit 24e229f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 23 deletions.
20 changes: 10 additions & 10 deletions nbconvert/exporters/qt_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def __init__(self, *args, **kwargs):
self.format = self.output_mimetype.split("/")[-1]
super().__init__(*args, **kwargs)

@default('file_extension')
@default("file_extension")
def _file_extension_default(self):
return "." + self.format

@default('template_name')
@default("template_name")
def _template_name_default(self):
return "qt" + self.format

@default('template_data_paths')
@default("template_data_paths")
def _template_data_paths_default(self):
return jupyter_path("nbconvert", "templates", "qt" + self.format)

Expand All @@ -31,8 +31,10 @@ def _check_launch_reqs(self):
from PyQt5.QtWidgets import QApplication
from .qt_screenshot import QtScreenshot
except ModuleNotFoundError as e:
raise RuntimeError(f"PyQtWebEngine is not installed to support Qt {self.format.upper()} conversion. "
f"Please install `nbconvert[qt{self.format}]` to enable.") from e
raise RuntimeError(
f"PyQtWebEngine is not installed to support Qt {self.format.upper()} conversion. "
f"Please install `nbconvert[qt{self.format}]` to enable."
) from e
return QApplication, QtScreenshot

def run_pyqtwebengine(self, html):
Expand All @@ -42,7 +44,7 @@ def run_pyqtwebengine(self, html):
temp_file = tempfile.NamedTemporaryFile(suffix=ext, delete=False)
filename = f"{temp_file.name[:-len(ext)]}.{self.format}"
with temp_file:
temp_file.write(html.encode('utf-8'))
temp_file.write(html.encode("utf-8"))

try:
QApplication, QtScreenshot = self._check_launch_reqs()
Expand All @@ -61,16 +63,14 @@ def run_pyqtwebengine(self, html):

def from_notebook_node(self, nb, resources=None, **kw):
self._check_launch_reqs()
html, resources = super().from_notebook_node(
nb, resources=resources, **kw
)
html, resources = super().from_notebook_node(nb, resources=resources, **kw)

self.log.info(f"Building {self.format.upper()}")
data = self.run_pyqtwebengine(html)
self.log.info(f"{self.format.upper()} successfully created")

# convert output extension
# the writer above required it to be html
resources['output_extension'] = f".{self.format}"
resources["output_extension"] = f".{self.format}"

return data, resources
9 changes: 5 additions & 4 deletions nbconvert/exporters/qt_screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class QtScreenshot(QWebEngineView):

def __init__(self, app):
super().__init__()
self.app = app
Expand All @@ -17,8 +16,7 @@ def capture(self, url, output_file, paginate):
self.loadFinished.connect(self.on_loaded)
# Create hidden view without scrollbars
self.setAttribute(QtCore.Qt.WA_DontShowOnScreen)
self.page().settings().setAttribute(
QWebEngineSettings.ShowScrollBars, False)
self.page().settings().setAttribute(QWebEngineSettings.ShowScrollBars, False)
if output_file.endswith(".pdf"):
self.export = self.export_pdf
self.page().pdfPrintingFinished.connect(lambda *args: self.app.exit())
Expand All @@ -41,7 +39,10 @@ def export_pdf(self):
page_layout = QPageLayout(page_size, QPageLayout.Portrait, QtCore.QMarginsF())
else:
factor = 0.75
page_size = QPageSize(QtCore.QSizeF(self.size.width() * factor, self.size.height() * factor), QPageSize.Point)
page_size = QPageSize(
QtCore.QSizeF(self.size.width() * factor, self.size.height() * factor),
QPageSize.Point,
)
page_layout = QPageLayout(page_size, QPageLayout.Portrait, QtCore.QMarginsF())

self.page().printToPdf(self.output_file, pageLayout=page_layout)
Expand Down
3 changes: 2 additions & 1 deletion nbconvert/exporters/qtpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class QtPDFExporter(QtExporter):
This inherits from :class:`HTMLExporter`. It creates the HTML using the
template machinery, and then uses pyqtwebengine to create a pdf.
"""

export_from_notebook = "PDF via HTML"

paginate = Bool(
Expand All @@ -24,7 +25,7 @@ class QtPDFExporter(QtExporter):
If False, a PDF with one long page will be generated.
Set to True to match behavior of LaTeX based PDF generator
"""
""",
).tag(config=True)

output_mimetype = "application/pdf"
1 change: 1 addition & 0 deletions nbconvert/exporters/qtpng.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class QtPNGExporter(QtExporter):
This inherits from :class:`HTMLExporter`. It creates the HTML using the
template machinery, and then uses pyqtwebengine to create a png.
"""

export_from_notebook = "PNG via HTML"

output_mimetype = "image/png"
1 change: 1 addition & 0 deletions nbconvert/exporters/tests/test_qtpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .base import ExportersTestsBase
from ..qtpdf import QtPDFExporter


class TestQtPDFExporter(ExportersTestsBase):
"""Contains test functions for qtpdf.py"""

Expand Down
1 change: 1 addition & 0 deletions nbconvert/exporters/tests/test_qtpng.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .base import ExportersTestsBase
from ..qtpng import QtPNGExporter


class TestQtPNGExporter(ExportersTestsBase):
"""Contains test functions for qtpng.py"""

Expand Down
8 changes: 4 additions & 4 deletions share/jupyter/nbconvert/templates/qtpdf/conf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"base_template": "lab",
"mimetypes": {
"application/pdf": true
}
"base_template": "lab",
"mimetypes": {
"application/pdf": true
}
}
8 changes: 4 additions & 4 deletions share/jupyter/nbconvert/templates/qtpng/conf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"base_template": "lab",
"mimetypes": {
"image/png": true
}
"base_template": "lab",
"mimetypes": {
"image/png": true
}
}

0 comments on commit 24e229f

Please sign in to comment.