9

For Matplotlib plots in iPython/Jupyter you can make the notebook plot plots inline with

%matplotlib inline

How can one do the same for NLTK draw() for trees? Here is the documentation http://www.nltk.org/api/nltk.draw.html

mikal94305
  • 4,663
  • 8
  • 31
  • 40
  • 2
    It looks like it implements its own drawing stuff based on Tkinter. If that's the case, there's no easy way to bring it inline in the notebook. – Thomas K Aug 05 '15 at 17:19

2 Answers2

10

Based on this answer:

import os
from IPython.display import Image, display
from nltk.draw import TreeWidget
from nltk.draw.util import CanvasFrame

def jupyter_draw_nltk_tree(tree):
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    tc['node_font'] = 'arial 13 bold'
    tc['leaf_font'] = 'arial 14'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    cf.add_widget(tc, 10, 10)
    cf.print_to_file('tmp_tree_output.ps')
    cf.destroy()
    os.system('convert tmp_tree_output.ps tmp_tree_output.png')
    display(Image(filename='tmp_tree_output.png'))
    os.system('rm tmp_tree_output.ps tmp_tree_output.png')

Little slow, but does the job. If you're doing it remotely, don't forget to run your ssh session with -X key (like ssh -X user@server.com) so that Tk could initialize itself (no display name and no $DISPLAY environment variable-kind of error)

UPD: it seems like last versions of jupyter and nltk work nicely together, so you can just do IPython.core.display.display(tree) to get a nice-looking tree-render embedded into the output.

Community
  • 1
  • 1
Ben Usman
  • 7,969
  • 6
  • 46
  • 66
  • Do you need to add any configs/magic? Which versions of jupyter, notebook, and nltk are you running to get embedded tree render? I cannot seem to get it, just updated to jupyter-client==4.4.0 jupyter-core==4.2.1 jupyterhub==0.7.2 notebook==4.3.1 I'm still getting tk errors: TclError: no display name and no $DISPLAY environment variable – Gena Kukartsev Jan 26 '17 at 23:50
  • @GenaKukartsev make sure you did `ssh -X` – Ben Usman Jan 27 '17 at 01:49
  • Are you saying I still need to do it for notebook? Just to be clear, I am not talking about command line use - that part is clear. What I want is NLTK graphics to appear inline in IPython notebook, similar to matplotlib graphics. This may be a problem for my setup: I am running a multiuser jupyterhub ipython notebook server. Users log in via web interface. Given that, if tk is still needed to enable embedded graphics, I am not sure how to set it up. Any suggestions? Thanks! – Gena Kukartsev Feb 01 '17 at 18:30
  • `multiuser jupyterhub` - never used that; I'm just saying that console session that you used to run the notebook sever must have X session attached to it, if you're using Linux; and no idea regarding Windows – Ben Usman Feb 01 '17 at 18:46
4

2019 Update:

This runs on Jupyter Notebook:

from nltk.tree import Tree
from IPython.display import display

tree = Tree.fromstring('(S (NP this tree) (VP (V is) (AdjP pretty)))')
display(tree)

Requirements:

  • NLTK
  • Ghostscript
bryant1410
  • 5,540
  • 4
  • 39
  • 40
Eric McLachlan
  • 3,132
  • 2
  • 25
  • 37