繁体   English   中英

尝试运行Flask Web App时获取未定义的错误为null

[英]Getting a null is not defined error when trying to run flask web app

我正在尝试构建一个网络应用程序,该应用程序将清理csv并以pdf格式返回图形。

由于我是构建Web应用程序和Flask的新手。 我试图通过构建一个简单的Web应用程序开始,该应用程序允许用户上传csv格式的文件。 但是,当我尝试在bash上运行该应用程序时,出现以下错误消息:

(venv) bash-3.2$ python main.py
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    "execution_count": null,
NameError: name 'null' is not defined

这是到目前为止我用来构建Web应用程序的代码。 我在Jupyter中编写了此代码,将其下载为.py文件,并尝试在bash上运行此文件

from pprint import pprint as pp
from flask import Flask, flash,redirect,render_template,request,url_for
from werkzeug.utils import secure_filename
upload_folder = '/path/to/the/uploads'
allowed_exts = set(['csv','pdf'])
app = Flask(__name__)
app.config['upload_folder'] = upload_folder
def allowed_f(file):
    return '.' in file and \
        file.rsplit('.',1)[1].lower() in allowed_exts
@app.route('/', methods = ['GET','POST'])
def upload():
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('File part cannot be found')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            flash('No file has been selected')
            return redirect(request.url)
        if file and allowed_f(file.filename):
            filename =  secure_filename(file.filename)
            file.save(os.path.join(app.config['upload_folder'],filename))
            return redirect(url_for('uploaded_file',filename=filename))
    return '''
    <!doctype html>
    <title>GMB Discovery Report builder/title>
    <h1>Upload GMB Discovery CSV</h1>
    <form method=post enctype=multipart/form-data>
    <input type=file name=file>
    <input type=submit value =Upload>
    </form>
    '''
from flask import send_from_directory
@app.route('/uploads/<filename>')
def uploaded_f(filename):
    return send_from_directory(app.config['upload_folder'],filename)
if __name__=='__main__':
    app.run(debug=True)

不知道我的代码的哪一部分导致此错误消息。

编辑在破折号上运行命令ipython main.py返回以下内容:

/Users/emmanuelsibanda/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:763: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
  warn("Attempting to work in a virtualenv. If you encounter problems, please "
Python 3.6.8 |Anaconda, Inc.| (default, Dec 29 2018, 19:04:46)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

您需要知道的所有内容都在错误消息中: main.py模块的第5行中包含"execution_count": null ,这会引发错误,因为显然您尚未定义名为null的变量。 也许您将其与内置的None混淆或打算使用字符串"null" (请注意引号),如果没有代码,这是无法分辨的。 您上面的代码段必须是其他代码,它不包含有问题的行。

如果您下载了Jupyter Notebook文件并以.py扩展名保存,则它不仅包含该脚本-只是嵌入该脚本的较大.ipynb结构的一小部分。 创建一个名为main.py的新文件,然后将脚本复制到该文件中,而不用下载笔记本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM