-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,6 @@ requirements: | |
- python | ||
- notebook | ||
- funcsigs | ||
- pypdf2 | ||
- ghost.py | ||
|
||
test: | ||
imports: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,3 @@ | |
ASSETS, | ||
APP_ROOT | ||
) | ||
from .html import PresentExporter | ||
from .pdf import PDFPresentExporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,11 @@ | ||
import os | ||
import shutil | ||
import sys | ||
from subprocess import check_call | ||
|
||
from ipython_genutils.tempdir import TemporaryWorkingDirectory | ||
import nbformat | ||
|
||
from .html import PresentExporter | ||
|
||
from nbbrowserpdf.exporters.pdf import BrowserPDFExporter | ||
|
||
class PDFPresentExporter(PresentExporter): | ||
def __init__(self, *args, **kwargs): | ||
super(PDFPresentExporter, self).__init__(*args, **kwargs) | ||
|
||
def from_notebook_node(self, nb, resources=None, **kw): | ||
output, resources = super(PDFPresentExporter, self).from_notebook_node( | ||
nb, resources=resources, **kw | ||
) | ||
|
||
with TemporaryWorkingDirectory() as td: | ||
for path, res in resources.get("outputs", {}).items(): | ||
dest = os.path.join(td, os.path.basename(path)) | ||
shutil.copyfile(path, dest) | ||
|
||
index_html = os.path.join(td, "index.html") | ||
|
||
with open(index_html, "w+") as fp: | ||
fp.write(output) | ||
|
||
ipynb = "notebook.ipynb" | ||
|
||
with open(os.path.join(td, ipynb), "w") as fp: | ||
nbformat.write(nb, fp) | ||
|
||
self.log.info("Building PDF...") | ||
check_call([ | ||
sys.executable, | ||
"-m", "nbpresent.exporters.pdf_capture", | ||
td | ||
]) | ||
|
||
pdf_file = "notebook.pdf" | ||
|
||
if not os.path.isfile(pdf_file): | ||
raise IOError("PDF creating failed") | ||
|
||
self.log.info("PDF successfully created") | ||
|
||
with open(pdf_file, 'rb') as f: | ||
pdf_data = f.read() | ||
|
||
# convert output extension to pdf | ||
# the writer above required it to be tex | ||
resources['output_extension'] = '.pdf' | ||
# clear figure outputs, extracted by latex export, | ||
# so we don't claim to be a multi-file export. | ||
resources.pop('outputs', None) | ||
|
||
return pdf_data, resources | ||
class PDFPresentExporter(PresentExporter, BrowserPDFExporter): | ||
def pdf_capture_args(self): | ||
return [ | ||
"--capture-server-class", | ||
"nbpresent.exporters.pdf_capture:SlideCaptureServer" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,83 @@ | ||
from concurrent import futures | ||
import os | ||
import logging | ||
import time | ||
import sys | ||
|
||
from ghost import Ghost | ||
from ghost.bindings import ( | ||
# QApplication, | ||
# QImage, | ||
QPainter, | ||
QPrinter, | ||
QtWebKit, | ||
QtCore, | ||
) | ||
|
||
|
||
import tornado.web | ||
from tornado.httpserver import HTTPServer | ||
|
||
from tornado.ioloop import IOLoop | ||
from tornado.concurrent import run_on_executor | ||
|
||
import nbformat | ||
|
||
from PyPDF2 import ( | ||
PdfFileReader, | ||
PdfFileWriter, | ||
PdfFileMerger, | ||
) | ||
|
||
from .base import DEFAULT_STATIC_FILES_PATH | ||
from nbbrowserpdf.exporters.pdf_capture import CaptureServer | ||
|
||
VIEWPORT = (1920, 1080) | ||
|
||
class CaptureServer(HTTPServer): | ||
executor = futures.ThreadPoolExecutor(max_workers=1) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(CaptureServer, self).__init__(*args, **kwargs) | ||
|
||
@run_on_executor | ||
def capture(self): | ||
# DO SOME MAGIC | ||
ghost = Ghost( | ||
log_level=logging.DEBUG | ||
) | ||
session = ghost.start( | ||
class SlideCaptureServer(CaptureServer): | ||
""" CaptureServer to generate multi-page PDF based on nbpresent metadata | ||
""" | ||
def init_session(self): | ||
""" create a session with a some tweaked settings | ||
""" | ||
return self.ghost.start( | ||
# display=True, | ||
viewport_size=(1920, 1080), | ||
# TODO: read this off config | ||
viewport_size=VIEWPORT, | ||
show_scrollbars=False, | ||
) | ||
merger = PdfFileMerger() | ||
join = lambda *bits: os.path.join(self.static_path, *bits) | ||
|
||
session.open("http://localhost:9999/index.html") | ||
def page_ready(self): | ||
""" Wait until nbpresent-css gets created | ||
""" | ||
self.session.wait_for_page_loaded() | ||
self.session.wait_for_selector("#nbpresent-css") | ||
time.sleep(1) | ||
|
||
try: | ||
session.wait_for_selector("#nbpresent-css") | ||
time.sleep(1) | ||
except Exception as err: | ||
print(err) | ||
def print_to_pdf(self, path): | ||
""" Custom print based on metadata: generate one per slide | ||
""" | ||
merger = PdfFileMerger() | ||
|
||
for i, slide in enumerate(self.notebook.metadata.nbpresent.slides): | ||
print("\n\n\nprinting slide", i, slide) | ||
filename = join("notebook-{0:04d}.pdf".format(i)) | ||
session.show() | ||
screenshot(self.notebook, session, filename) | ||
# science help you if you have 9999 slides | ||
filename = self.in_static("notebook-{0:04d}.pdf".format(i)) | ||
# this is weird, but it seems to always need it | ||
self.session.show() | ||
self.screenshot(filename) | ||
merger.append(PdfFileReader(filename, "rb")) | ||
result, resources = session.evaluate( | ||
|
||
# advance the slides | ||
result, resources = self.session.evaluate( | ||
""" | ||
console.log(window.nbpresent); | ||
console.log(window.nbpresent.mode.presenter.speaker.advance()); | ||
""") | ||
time.sleep(1) | ||
|
||
merger.write(join("notebook-unmeta.pdf")) | ||
|
||
unmeta = PdfFileReader(join("notebook-unmeta.pdf"), "rb") | ||
|
||
meta = PdfFileWriter() | ||
meta.appendPagesFromReader(unmeta) | ||
|
||
ipynb = "notebook.ipynb" | ||
# always seem to get some weirdness... perhaps could inttegrate | ||
# ioloop... | ||
time.sleep(1) | ||
|
||
with open(join(ipynb), "rb") as fp: | ||
meta.addAttachment(ipynb, fp.read()) | ||
# all done! | ||
merger.write(path) | ||
|
||
with open(join("notebook.pdf"), "wb") as fp: | ||
meta.write(fp) | ||
def screenshot(self, filename): | ||
""" do an individual slide screenshot | ||
big thanks to https://gist.github.com/jmaupetit/4217925 | ||
""" | ||
|
||
raise KeyboardInterrupt() | ||
printer = QPrinter(mode=QPrinter.ScreenResolution) | ||
printer.setOutputFormat(QPrinter.PdfFormat) | ||
printer.setPaperSize(QtCore.QSizeF(*reversed(VIEWPORT)), | ||
QPrinter.DevicePixel) | ||
printer.setOrientation(QPrinter.Landscape) | ||
printer.setOutputFileName(filename) | ||
printer.setPageMargins(0, 0, 0, 0, QPrinter.DevicePixel) | ||
|
||
def screenshot(nb, session, dest, as_print=False): | ||
""" | ||
big thanks to https://gist.github.com/jmaupetit/4217925 | ||
""" | ||
|
||
printer = QPrinter(mode=QPrinter.ScreenResolution) | ||
printer.setOutputFormat(QPrinter.PdfFormat) | ||
printer.setPaperSize(QtCore.QSizeF(1080, 1920), QPrinter.DevicePixel) | ||
printer.setOrientation(QPrinter.Landscape) | ||
printer.setOutputFileName(dest) | ||
printer.setPageMargins(0, 0, 0, 0, QPrinter.DevicePixel) | ||
|
||
if as_print: | ||
webview = QtWebKit.QWebView() | ||
webview.setPage(session.page) | ||
webview.print_(printer) | ||
else: | ||
painter = QPainter(printer) | ||
painter.scale(1.45, 1.45) | ||
session.main_frame.render(painter) | ||
self.session.main_frame.render(painter) | ||
painter.end() | ||
|
||
|
||
def pdf_capture(static_path): | ||
settings = { | ||
"static_path": static_path | ||
} | ||
|
||
app = tornado.web.Application([ | ||
(r"/components/(.*)", tornado.web.StaticFileHandler, { | ||
"path": os.path.join(DEFAULT_STATIC_FILES_PATH, "components") | ||
}), | ||
(r"/(.*)", tornado.web.StaticFileHandler, { | ||
"path": settings['static_path'] | ||
}), | ||
], **settings) | ||
|
||
server = CaptureServer(app) | ||
server.static_path = static_path | ||
|
||
with open(os.path.join(static_path, "notebook.ipynb")) as fp: | ||
server.notebook = nbformat.read(fp, 4) | ||
|
||
ioloop = IOLoop() | ||
ioloop.add_callback(server.capture) | ||
server.listen(9999) | ||
|
||
try: | ||
ioloop.start() | ||
except KeyboardInterrupt: | ||
print("stopped") | ||
|
||
if __name__ == "__main__": | ||
pdf_capture(sys.argv[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.