专业网站建设全包,y-m-d WordPress 首页,化妆品网站设计,小企业做网站有没有用文章目录 1、flask后端接收来自前端的数据1#xff09;如果前端提交的方法为POST2#xff09;如果前段提交的方法是GET 2、flask后端向前端传数据3、案例参考文献 1、flask后端接收来自前端的数据
1#xff09;如果前端提交的方法为POST
后端接收时的代码#xff1a;
xx… 文章目录 1、flask后端接收来自前端的数据1如果前端提交的方法为POST2如果前段提交的方法是GET 2、flask后端向前端传数据3、案例参考文献 1、flask后端接收来自前端的数据
1如果前端提交的方法为POST
后端接收时的代码
xxrequest.form.get(xx);
xxrequest.form[xx]2如果前段提交的方法是GET
xxrequest.args.get(xx)2、flask后端向前端传数据
传送单个数据
return render_template(html文件,xxxx)传送多个数据先把数据写进字典字典整体进行传输
return render_template(html文件,xx字典变量)3、案例
目录结构 index.py文件
# --*-- coding:utf-8 --*--
# Author : 一只楚楚猫
# File : index.py
# Software : PyCharmfrom flask import *
from sentence_transformers import SentenceTransformer
import torch.nn as nn
import torch
import torch.nn.functional as Fmodel SentenceTransformer(rE:\楚楚猫\code\python\01design\01creativity\01distance\all-MiniLM-L6-v2)app Flask(__name__)result dict()
result[results] app.route(/, methods(GET, POST))
def index():global resultif request.method POST:step1 request.form.get(step1)step2 request.form.get(step2)step3 request.form.get(step3)step4 request.form.get(step4)# 用户输入的内容sentences [step1, step2, step3, step4]results list()# 384维embeddings torch.FloatTensor(model.encode(sentences))# p2就是计算欧氏距离p1就是曼哈顿距离euclidean_distance nn.PairwiseDistance(p2)for i in range(0, embeddings.size()[0]):for j in range(i 1, embeddings.size()[0]):cosine_similarity round(F.cosine_similarity(embeddings[i], embeddings[j], dim0).item(), 4)distance round(euclidean_distance(embeddings[i], embeddings[j]).item(), 4)results.append(fstep{i 1} step{j 1}的相关性{cosine_similarity} step{i 1} step{j 1}的距离{distance})print(fstep{i 1} step{j 1}之间的相关性{cosine_similarity}step{i 1} step{j 1}之间的距离{distance})result[results] resultsreturn render_template(hello.html, resultresult)return render_template(hello.html, resultresult)if __name__ __main__:app.run(port11252)
hello.html文件
!DOCTYPE html
html langen
meta charsetutf-8
headmeta nameviewport contentwidthdevice-width, initial-scale1.0, maximum-scale1.0, user-scalableno/meta http-equivcontent-type contenttext/html;charsetutf-8meta http-equivX-UA-Compatible contentIEedge,chrome1title欢迎来到我的世界/titlelink href../static/css/style.css relstylesheet typetext/css
/headdiv idcontainerdiv idoutputdiv classcontainerTh1Yimo/h1form classform identry_form methodposth2input typetext namestep1 styleheight:30px/h2brh2input typetext namestep2 styleheight:30px/h2brh2input typetext namestep3 styleheight:30px/h2brh2input typetext namestep4 styleheight:30px/h2brcenterbuttonh3Click me!(๑•̀ㅂ•́)و✧/h3/button/center{{result.results}}/form/div/div
/div/body
/html上面涉及到的sentence_transformers模块来自论文《Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks》使用孪生网络Siamse Network将两个文本映射到特征空间得到特征向量384维
参考文献
1、flask 前端html与后端python相互传数据https://www.cnblogs.com/zzai/p/html_dataStream.html 2、如何从Python发布数据并进入Flask前端https://www.cnpython.com/qa/1513860