Python flask框架如何顯示圖像到web頁面
代碼如下
webfig1.py
from flask import Flask from flask import render_template import matplotlib.pyplot as plt import io import base64 app = Flask(__name__) @app.route('/') def build_plot(): img = io.BytesIO() y = [1,2,3,4,5] x = [0,2,1,3,4] plt.plot(x,y) plt.savefig(img, format='png') img.seek(0) plot_url = base64.b64encode(img.getvalue()).decode() return render_template('plot.html', plot_url=plot_url) if __name__ == '__main__': app.debug = True app.run()
plot.html
<!DOCTYPE html> <html> <title> Plot</title> <body> <img src="data:image/png;base64, {{ plot_url }}"> </body> </html>
先用py繪制了xy的圖像,然后經(jīng)過幾個命令,讓其轉(zhuǎn)化為plot_url,在傳給plot.html,就可以了
代碼在github:https://github.com/qingnvsue/flask中的webfig文件夾
我自己的程序是在網(wǎng)頁輸入sin函數(shù)的幅度,頻率,自變量范圍等,然后繪制這個sin函數(shù),讓其顯示到web頁面,如圖
代碼在github:https://github.com/qingnvsue/flask中的sin文件夾
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python?sklearn?中的?make_blobs()?函數(shù)示例詳解
make_blobs()?是?sklearn.datasets中的一個函數(shù),這篇文章主要介紹了Python?sklearn?中的?make_blobs()?函數(shù),本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02詳解Golang 與python中的字符串反轉(zhuǎn)
這篇文章主要介紹了詳解Golang 與python中的字符串反轉(zhuǎn)的相關(guān)資料,這里提供了實現(xiàn)的實例以便大家學(xué)習(xí)理解,需要的朋友可以參考下2017-07-07pandas中std和numpy的np.std區(qū)別及說明
這篇文章主要介紹了pandas中std和numpy的np.std區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08