python - 如何在网站上实现 Python 运行时和图形?
问题描述
我想问一下如何在网站上实现 Python 运行时和图形,如 Udacity的 PD 控制器课程中所见?
如果在本地运行,matplotlib 将生成一个包含图形的弹出窗口。它是如何捕获并显示在网站上的?文本输出显示在终端中。在 Udacity 上,所有这些都显示在一个页面中。它们是如何被捕获和显示的?如果您想显示由乌龟图形等生成的实时动画怎么办?
以及如何提供一个代码输入区域以及向用户显示的代码演示?以及如何在 math.stackexchange.com 等页面上提供 LaTex 等功能?
您是否必须使用某些框架、API 和语言,或者它是否独立于所有这些?
解决方案
您需要使用 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
推荐阅读
- java - 如何使用java将两种不同大小的列表发送到excel中
- node.js - 如何在 Vagrant VM 上安装 bcrypt?
- wpf - Ignoring Button Clicks without Disabling Button
- sql - On delete cascade with 2 different tables
- angular - Angular5垫选择
- c# - 局部函数和 SOLID 原则 C#
- python - Paramiko failing due due to file permissions
- javascript - 如何在不影响输入值的情况下将美元符号添加到输入字段?
- python - 使用 Python argparse 拾取的参数管道到 Docker 标准输入
- cordova - 限制相机插件ionic2 cordova中的文件大小?