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

Add PDF display convenience function #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 docs/conf.py
Expand Up @@ -62,4 +62,4 @@

master_doc = "index"

html_sidebars = { '**': ['globaltoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html'] }
html_sidebars = {"**": ["globaltoc.html", "relations.html", "sourcelink.html", "searchbox.html"]}
Binary file modified examples/annotated_plot.pdf
Binary file not shown.
27 changes: 23 additions & 4 deletions examples/example.ipynb
Expand Up @@ -49,7 +49,24 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/html": [
"<embed src=annotated_plot.pdf style=\"position: relative; height: 100%; width: 500px;\" type=\"application/x-google-chrome-pdf\"></embed>"
],
"text/latex": [
"\\includegraphics[width=1.0\\columnwidth]{annotated_plot.pdf}"
],
"text/plain": [
"<rsmf.show_pdf.PDF at 0x1f2fa2af2c8>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = np.linspace(0, 5, 50)\n",
"f = (x-1)**2 - x + 0.25 + np.sin(3*x) + 5 * np.exp(-x)\n",
Expand All @@ -64,7 +81,9 @@
"plt.text(1.5,7.5, r'$\\int_0^x \\mathrm{d}\\xi \\, f(\\xi)$')\n",
"plt.xlabel('$x$')\n",
"plt.tight_layout()\n",
"plt.savefig(\"annotated_plot.pdf\")"
"plt.savefig(\"annotated_plot.pdf\")\n",
"\n",
"rsmf.show(\"annotated_plot.pdf\")"
]
},
{
Expand Down Expand Up @@ -203,9 +222,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5-final"
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
}
3 changes: 2 additions & 1 deletion rsmf/__init__.py
Expand Up @@ -2,4 +2,5 @@
Load all methods that are made accessible as the API.
"""
from .custom_formatter import CustomFormatter
from .setup import setup
from .setup import setup
from .show_pdf import show
59 changes: 59 additions & 0 deletions rsmf/show_pdf.py
@@ -0,0 +1,59 @@
"""
Add convenience methods to show PDFs in Jupyter Notebook.
"""

class PDF(object):
"""
Provides a wrapper around a PDF file path that can be displayed in Jupyter Notebooks.

Based on ``https://stackoverflow.com/questions/19470099/view-pdf-image-in-an-ipython-notebook``.

Args:
pdf_path (str): Path of the PDF file to be displayed
width (Union[int,str,NoneType], optional): Width of the displayed PDF's iframe.
Considered as pixels when integer, and as CSS compatible width measurement when string. Defaults to 100%.
height ([type], optional): Height of the displayed PDF's iframe.
Considered as pixels when integer, and as CSS compatible height measurement when string. Defaults to 500px.
"""

def __init__(self, pdf_path, width=None, height=None):
self.pdf_path = pdf_path

if isinstance(width, str):
self.width_str = width
elif isinstance(width, int):
self.width_str = "{}px".format(width)
else:
self.width_str = "100%"

if isinstance(height, str):
self.height_str = height
elif isinstance(height, int):
self.height_str = "{}px".format(height)
else:
self.height_str = "500px"

def _repr_html_(self):
return '<iframe src={0} style="position: relative; height: {1}; width: {2};"></iframe>'.format(
self.pdf_path, self.width_str, self.height_str
)

def _repr_latex_(self):
return r"\includegraphics[width=1.0\columnwidth]{{{0}}}".format(self.pdf_path)


def show(pdf_path, width=None, height=None):
"""
Show a PDF in Jupyter Notebook.

Args:
pdf_path (str): Path of the PDF file to be displayed
width (Union[int,str,NoneType], optional): Width of the displayed PDF's iframe.
Considered as pixels when integer, and as CSS compatible width measurement when string. Defaults to 100%.
height ([type], optional): Height of the displayed PDF's iframe.
Considered as pixels when integer, and as CSS compatible height measurement when string. Defaults to 300px.

Returns:
PDF: A wrapper around the the PDF file's path that renders as an iframe in Jupyter Notebook.
"""
return PDF(pdf_path, width=width, height=height)