我正在尝试将一些 pandas 数据框表导出为图像。我查看了许多解决方案,并找到了看起来最好的选择。我按照 this site 上的说明进行操作,并且在本地一切正常。
但是,当试图让这个 python 脚本在 EC2 服务器上运行时,我收到以下错误:
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 报告的表格图像?
部分代码:
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
然后重新运行您的原始命令
注意:连接到远程服务器时使用ssh -X
关于python - EC2 服务器上的 "Could not connect to display",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56264534/