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

Tree drawing doesn't work on a headless server #1887

Open
DavidNemeskey opened this issue Nov 14, 2017 · 7 comments
Open

Tree drawing doesn't work on a headless server #1887

DavidNemeskey opened this issue Nov 14, 2017 · 7 comments

Comments

@DavidNemeskey
Copy link

nltk.tree.Tree uses the Tcl (tkinter) backend to draw trees. Unfortunately, that doesn't seem to work on a headless server, which is the setup I use: run Jupyter on the server and access it via the browser from another machine.

If python3-tk is not installed, I get

ImportError: No module named '_tkinter', please install the python3-tk package

If it is, I get

TclError: no display name and no $DISPLAY environment variable

However, dot works in such a setup, so it would be preferable if it was used as the backend instead of tk. Also, it could be used to implement _repr_svg_() on Tree, which would also be a very welcome addition, as svg is a much better format for these kinds of things than png.

@rmalouf
Copy link
Contributor

rmalouf commented Nov 14, 2017

nltk.tree.Tree._repr_png_ also depends on ghostscript, which might be a problem. E.g., it doesn't get appear to get installed as part of the default anaconda environment.

@rawlins
Copy link

rawlins commented Oct 16, 2018

This is also an issue for running anything that might draw a tree through CI systems; e.g. I test my notebooks for a project on travis and was hitting it. The super hacky workaround I came up with for this scenario, where it's not really crucial that the tree get drawn, is at: rawlins/lambda-notebook@7f151a0

Edit: true to form, I figured out what I was missing within 30 seconds of posting here, so there's a slightly better (but still hacky) thing at the link above, vs. what I originally posted.

@anatoly-khomenko
Copy link

anatoly-khomenko commented Sep 11, 2019

I managed to find a workaround for my configuration (Ubuntu 14 headless server), by installing python3-tk and xvfb and running jupyter notebook with xvfb-run.
Exact commands are:

sudo apt-get install python3-tk
sudo apt-get install xvfb
xvfb-run --auto-servernum --server-num=1 jupyter notebook --allow-root

I used the following code to test (note that tree.draw() will hang, as it is interactive):

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

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

nltk-tree-jupyter

@rawlins
Copy link

rawlins commented Sep 11, 2019

I guess I never came back and posted in this issue, but I later ended up writing a pure python svg-based tree drawing package that works with nltk, and as a side-effect of being python-only also solves the problem of headless servers: https://github.com/rawlins/svgling

For Jupyter in particular, you will also need to fully disable nltk png-based trees, since Jupyter runs all available reprs in the background even though it shows only one; there's a snippet on that page showing how.

@pomkos
Copy link

pomkos commented Jul 17, 2021

The svgling package works great, but wondering if there is an inbuilt solution since 2019?

@rawlins
Copy link

rawlins commented Sep 25, 2023

This is more or less resolved by #2875 right? (If someone still wanted raster images in a headless setting, there's also now a straightforward conversion path to png via svgling+cairosvg.)

@DavidNemeskey
Copy link
Author

I don't use nltk anymore, but for what it's worth, I agree. My original problem would have been solved by svgling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants