-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create gmt.show to embed plots in notebook
Saves them to a temp file and loads them with the IPython.display machinery. Fixes #19
- Loading branch information
Showing
6 changed files
with
72 additions
and
38 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
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Extra modules that are exclusive to the Python API (for things like the Jupyter | ||
notebook or simpler interfaces). | ||
""" | ||
import tempfile | ||
import os | ||
|
||
try: | ||
from IPython.display import Image, display | ||
except ImportError: | ||
Image, display = None, None | ||
|
||
from . import psconvert | ||
|
||
|
||
def show(dpi=100, width=500, return_img=False): | ||
""" | ||
Display the last figure in the Jupyter notebook | ||
You will need to have IPython installed for this to work. You should have | ||
it if you are using a Jupyter notebook. | ||
Parameters | ||
---------- | ||
dpi : int | ||
The image resolution (dots per inch). | ||
width : int | ||
Width of the figure shown in the notebook in pixels. | ||
return_img : bool | ||
Whether or not to return the IPython.display.Image variable. | ||
""" | ||
assert Image is not None and display is not None, \ | ||
"Couldn't find IPython. Please make sure it's installed." | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
prefix = os.path.join(tmpdir, 'gmt-figure-for-notebook') | ||
fname = prefix + '.png' | ||
psconvert(prefix=prefix, fmt='G', dpi=dpi, | ||
crop=True, portrait=True) | ||
img = Image(filename=fname, embed=True, width=width) | ||
display(img) | ||
if return_img: | ||
return img |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
Test some of the extra modules from the Python API | ||
""" | ||
import os | ||
|
||
from ..extra_modules import show | ||
from .. import figure, psbasemap | ||
|
||
|
||
def test_show(): | ||
"Test that show creates the correct file name and deletes the temp dir" | ||
figure() | ||
psbasemap(R='10/70/-300/800', J='X3i/5i', B='af', | ||
D='30/35/-200/500', F=True) | ||
img = show(width=800, return_img=True) | ||
assert os.path.split(img.filename)[-1] == 'gmt-figure-for-notebook.png' | ||
assert not os.path.exists(img.filename) | ||
assert img.width == 800 |