4

I'm trying to export some pandas dataframe tables as images. I looked at many solutions, and found what seemed like the best bet. I followed the directions on this site, and everything worked fine locally.

However, when trying to get this python script to run on an EC2 server, I get the following error:

OSError: wkhtmltoimage exited with non-zero code 1. error:
qt.qpa.screen: QXcbConnection: Could not connect to display 
Could not connect to any X display.


You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.

I tried install to xfvb as directed, but that doesn't work (I imagine because wkhtmltopdf is being called from another library). I spun up a new server to re-install everything from scratch to eliminate that potential issue. Still nothing.

I Googled the issue and tried some random suggestions, but no dice.

My goal is to produce an SVG file from a pandas dataframe (converted to html). Is this possible to do form a cloud server with no monitor? Is there a better way to produce table images for PDF reports from pandas?

Some code:

import pandas
data = pandas.read_csv(open("biostats.csv", "r"))

css = """
<style type=\"text/css\">
table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
width: 640px;
border-collapse:
collapse; 
border-spacing: 0;
}"""

text_file = open("filename.html", "a")
# write the CSS
text_file.write(css)
# write the HTML-ized Pandas DataFrame
text_file.write(data.to_html())

imgkitoptions = {"format": "svg"}
imgkit.from_file("filename.html", outputfile, options=imgkitoptions)
text_file.close()
NoobsterNoob
  • 125
  • 1
  • 2
  • 7
  • Two suggestions come to mind: 1. save the DataFrame in Excel or CSV format on the EC2 instance, then copy it to your local machine via SCP (using either command-line scp or a GUI client such as WinSCP, FileZilla, or CuberDuck). 2. connect from your local machine to the Jupyter server running on the EC2 instance via SSH tunneling (probably helpful: https://gist.github.com/jakechen/faf0500132d46d83517004bbfedbe5de) – Peter Leimbigler May 22 '19 at 20:44
  • Thanks for the suggestion. I'm currently doing something along those lines (manually transferring the file), but I'm trying to get the images to generate automatically, via crontab or similar. I guess I should have noted that in the question! – NoobsterNoob May 22 '19 at 21:34

2 Answers2

2

try doing echo $DISPLAY on your remote host. If it turns out empty, it means you are not connected to any display screen and hence, the output will not be displayed.

If that's the case try doing export DISPLAY=localhost:10.0 and then re-run your original command

Note: when connecting to the remote server use ssh -X

  • Wow, that worked when running it manually. My question now is, how do I get it to work in a crontab? The error persists if I run it in the crontab. Is that possible? – NoobsterNoob May 22 '19 at 21:29
  • I'm not very experienced with crontab but I have added another solution, which I feel may be of help. You could try that! :) Also, if this solution worked for you in the context of the question, please upvote and/or accept this answer :) – Prachiti Prakash Prabhu May 22 '19 at 21:48
2

One solution would be to run this:

from pyvirtualdisplay import Display
display = Display(visible=0, size=(600,600))
display.start()
imgkit.from_file("filename.html", outputfile, options=imgkitoptions)