问题
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()
回答1:
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
回答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)
来源:https://stackoverflow.com/questions/56264534/could-not-connect-to-display-on-ec2-server