我的任务是在Python Jupyter笔记本中构建一个回归模型,并希望将其“ 生产环境 ”到一个简单的网站中 . 我陷入了如何将我的回归模型与Flask应用程序结合起来的问题 .

理想情况下,我希望网页能够:

  • 提示用户在网页中输入一些值,例如他们想要调查的促销类型 . (这已经完成了吗?)

  • 使用Python脚本生成结果,而用户输入将作为此脚本的一些参数 .

  • 自动(或使用按钮)将结果下载到本地计算机 .

Here is what I already have:

  • 一个python脚本 run_model.py ,当使用 python run_model.py 从终端调用时会输出一些.csv文件和一些.jpg文件 .

  • 文件夹 example 中的Flask应用程序,其中包含

  • static 文件夹中的.csv文件,

  • template 文件夹中的一些模板 .

  • 还有一个 script.py 文件似乎是主要的Flask应用程序 . 即当我在终端中调用 python script.py 时,将在我的localhost上运行一个开发服务器 .

  • 我还有以下在页面中实现"download"按钮的函数 . 即当用户点击"download"时,网站将下载"static"文件夹中的.xlsx文件 .

  • 我有一个功能,允许用户将某些内容(当前是他们的电子邮件地址)输入到Web表单中并提交 . 然后Python列表将记录此信息并打印到另一个网页 .

这是我在 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>