任务是我已经在Python Jupyter笔记本中构建了一个回归模型,并希望将其“生产”到一个简单的网站中。我陷入了如何将我的回归模型与Flask应用程序结合的问题。
理想情况下,我希望网页为:
这是我已经拥有的:
run_model.py
,当从终端使用python run_model.py
进行调用时,它将输出一些.csv文件和一些.jpg文件。example
中的Flask应用,其中包含
static
文件夹中的.csv文件template
文件夹中的一些模板。 script.py
文件,它似乎是Flask的主要应用程序。即,当我在终端中呼叫python script.py
时,本地主机上将运行一个开发服务器。这是我在script.py
中拥有的烧瓶功能...
from flask import Flask, request, render_template, redirect, send_file
app = Flask(__name__)
email_addresses = []
@app.route('/')
def welcome():
author = "ABC"
name = "Market Attribution Tool"
return render_template('index.html', author=author, name=name)
@app.route('/input_brand', methods = ['POST'])
def input_brand():
email = request.form['email']
email_addresses.append(email)
print(email_addresses)
return redirect('/')
@app.route('/emails.html')
def emails():
return render_template('emails.html', email_addresses=email_addresses)
@app.route('/file-downloads/')
def file_downloads():
try:
return render_template('downloads.html')
except Exception as e:
return str(e)
@app.route('/return-files/')
def return_files_tut():
try:
return send_file('static/prime.xlsx', attachment_filename='prime.xlsx')
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run()
这是index.html
脚本:
<!DOCTYPE html>
<html>
<head>
<title>{{ author }}'s app</title>
</head>
<body>
<h2>Welcome to ABC{{ name }}!</h2>
Brought to you by {{author}}.
<form action="/input_brand" method="post">
<input type="text" name="email"></input>
<input type="submit" value="Input brand"></input>
</form>
<form action="/download" method="post">
<input type="submit" value="Download data"></input>
</form>
</body>
</html>
这是download.html
脚本:
<!DOCTYPE html>
<html>
<head>
<title>Download page.</title>
</head>
<body class="body">
<div class="container" align="left">
<a href="/return-files/" target="blank"><button class='btn btn-default'>Download!</button></a>
</div>
</html>