"Could not connect to display" 在 EC2 服务器上

"Could not connect to display" on EC2 Server

我正在尝试将一些 pandas 数据帧 table 导出为图像。我查看了许多解决方案,并找到了看起来最好的选择。我按照 this site 上的说明进行操作,在本地一切正常。

但是,当我试图在 EC2 服务器上将此 python 脚本获取到 运行 时,出现以下错误:

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": ""}.

我尝试按照指示安装到 xfvb,但这不起作用(我想是因为 wkhtmltopdf 是从另一个库调用的)。我启动了一台新服务器,从头开始重新安装所有内容,以消除该潜在问题。还是没有。

我用 Google 搜索了这个问题并尝试了一些随机建议,但没有成功。

我的目标是从 pandas 数据帧(转换为 html)生成一个 SVG 文件。是否可以在没有监视器的情况下形成云服务器?有没有更好的方法从 pandas 为 PDF 报告生成 table 图像?

一些代码:

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()

尝试在您的远程主机上执行 echo $DISPLAY。如果结果为空,则表示您没有连接到任何显示屏,因此不会显示输出。

如果是这种情况,请尝试执行 export DISPLAY=localhost:10.0 然后 re-run 您的原始命令

注意:连接到远程服务器时使用ssh -X

一个解决方案是 运行 这个:

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