【问题标题】:How to implement Python run time and graphics on website?如何在网站上实现 Python 运行时和图形?
【发布时间】:2020-10-13 04:38:29
【问题描述】:

我想问一下如何在 PD 控制器上的 Udacity lesson 中看到如何在网站上实现 Python 运行时和图形?

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

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

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

【问题讨论】:

    标签: python html css api web


    【解决方案1】:

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

    app.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)
    

    index.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
    

    【讨论】:

    • 像海龟图形这样的实时动画怎么样?
    【解决方案2】:

    Savefig

    因此,您的问题的答案将不是plt.show(),而是在后端将plt.savefig() 作为临时文件,然后将其添加到网站。有像 svg 这样的保存格式,它们也将为您提供在前端调整大小和渲染的所有好处。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2016-08-18
      • 1970-01-01
      • 2011-03-06
      • 2020-08-07
      相关资源
      最近更新 更多