python - EC2 服务器上的 "Could not connect to display"

标签 python pandas wkhtmltopdf

我正在尝试将一些 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/

相关文章:

python - Networkx 寻找有向图的社区

python - Python Max/Min 函数中的 iter 和 key 如何工作?

python - 使用 Pandas .diff() 计算时间序列的二阶导数

python - 如何 append 多个 CSV 文件并在 Python 中添加一个指示文件名的 append 列?

batch-file - 如何使用 INPUT/OUTPUT in Progress 4GL 运行 .exe/.bat 文件

python - django 测试中的修补(模拟)表单

python - pandas:TimeGrouper 的文档在哪里?

Python Pandas Dataframe - 如果标志为 '1',将数据转换为 NaN 的最快方法是什么?

html - Wkhtmltopdf 输出不一致

javascript - 如何在 Node js 中发送文件以作为响应下载