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

Call PDF viewer after latex compiling (nbconvert) #4348

Merged
merged 2 commits into from
Oct 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion IPython/nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def _log_level_default(self):

def _classes_default(self):
classes = [NbConvertBase, ProfileDir]
for pkg in (exporters, preprocessors, writers):
for pkg in (exporters, preprocessors, writers, postprocessors):
for name in dir(pkg):
cls = getattr(pkg, name)
if isinstance(cls, type) and issubclass(cls, Configurable):
Expand Down
19 changes: 18 additions & 1 deletion IPython/nbconvert/postprocessors/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import subprocess
import os
import sys

from IPython.utils.traitlets import Integer, List, Bool

Expand Down Expand Up @@ -44,6 +45,9 @@ class PDFPostProcessor(PostProcessorBase):
config=True, help="""
Filename extensions of temp files to remove after running.
""")
pdf_open = Bool(False, config=True, help="""
Whether or not to open the pdf after the compile call.
""")

def run_command(self, command_list, filename, count, log_function):
"""Run command_list count times.
Expand Down Expand Up @@ -113,6 +117,16 @@ def clean_temp_files(self, filename):
except OSError:
pass

def open_pdf(self, filename):
"""Open the pdf in the default viewer."""
if sys.platform.startswith('darwin'):
subprocess.call(('open', filename))
elif os.name == 'nt':
os.startfile(filename)
elif os.name == 'posix':
subprocess.call(('xdg-open', filename))
return

def postprocess(self, filename):
"""Build a PDF by running pdflatex and bibtex"""
self.log.info("Building PDF")
Expand All @@ -128,5 +142,8 @@ def postprocess(self, filename):
filename = os.path.splitext(filename)[0]
if os.path.isfile(filename+'.pdf'):
self.log.info('PDF successfully created')
if self.pdf_open:
self.log.info('Viewer called')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to put the log statement before taking the action rather than after, otherwise you don't get any useful information if something goes wrong (e.g. a posix system where xdg-open doesn't exist).

self.open_pdf(filename+'.pdf')
return