我整理了一个非常小的烧瓶应用程序示例来复制我的问题。下面的代码生成了一个简单的图,我想在out.html文件中显示它。但是,运行该应用程序会创建一个图,但会在其自己的窗口中显示。
如何强制将绘图绘制在out.html文件中大约所需的位置?以下是最小烧瓶应用程序和带有jinga2的最小html文件。
from flask import Flask, render_template, request, session
from flask_session import Session
import plotly.express as px
server = Flask(__name__)
@server.route('/')
def visual():
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')
return render_template('out.html', out = fig.show())
if __name__ == '__main__':
server.run(debug=True)
out.html的内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<h1>
I want the figure to be about here
</h1>
{{out}}
</body>
</html>
答案 0 :(得分:0)
用out = fig.show()
代替out = fig.to_html(full_html=False)
然后在您的 HTML 文件中,使用 {{ out }}
代替 {{ out|safe }}
更多信息:https://plotly.com/python-api-reference/generated/plotly.io.to_html.html