首页 > 解决方案 > 如何在网站上实现 Python 运行时和图形?

问题描述

我想问一下如何在网站上实现 Python 运行时和图形,如 Udacity的 PD 控制器课程中所见?

如果在本地运行,matplotlib 将生成一个包含图形的弹出窗口。它是如何捕获并显示在网站上的?文本输出显示在终端中。在 Udacity 上,所有这些都显示在一个页面中。它们是如何被捕获和显示的?如果您想显示由乌龟图形等生成的实时动画怎么办?

以及如何提供一个代码输入区域以及向用户显示的代码演示?以及如何在 math.stackexchange.com 等页面上提供 LaTex 等功能?

您是否必须使用某些框架、API 和语言,或者它是否独立于所有这些?

图像

标签: pythonhtmlcssapiweb

解决方案


您需要使用 python 后端框架,我使用Django或 Flask 并在我的后端运行 mathplotlib 并使用 HTML img 标签以图像形式显示它这是烧瓶中的示例

应用程序.py

from flask import Flask, render_template
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4, 5])
plt.ylabel('some numbers')
plt.savefig('static/graph.png')
app = Flask(__name__)


@app.route('/')
def home():
    return render_template("index.html")


if __name__ == "__main__":
    app.run(debug=True)

索引.html

<!DOCTYPE html>
<html>
    <head>
        <title>MathPlotLib</title>
    </head>
    <body>
        <div><img alt="Sample Work" src="{{ url_for('static', filename='graph.png') }}">
        </div>
    </body>
</html>

文件系统

Main Folder
----templates
    ----index.html
----static
    ----graph.png
----app.py

推荐阅读