[英]Getting a null is not defined error when trying to run flask web app
I am trying to build a web app that will clean up a csv and return graphs in pdf format. 我正在尝试构建一个网络应用程序,该应用程序将清理csv并以pdf格式返回图形。
Since I am new to building web apps and flask in general. 由于我是构建Web应用程序和Flask的新手。 I am trying to start off by building a simple web app that allows users to upload files of csv format.
我试图通过构建一个简单的Web应用程序开始,该应用程序允许用户上传csv格式的文件。 However, when I try to run the app on bash I get this error message:
但是,当我尝试在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
Here is the code I have used to build the web app I have thus far. 这是到目前为止我用来构建Web应用程序的代码。 I wrote this code in Jupyter, downloaded it as a .py file and tried running this file on bash
我在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)
Not sure which part of my code is resulting in this error message. 不知道我的代码的哪一部分导致此错误消息。
Edit running the command ipython main.py on dash returned this: 编辑在破折号上运行命令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.
Everything you need to know is right there in the error message: Line 5 of your main.py
module has "execution_count": null
in it, which raises an error because apparently you have not defined a variable named null
. 您需要知道的所有内容都在错误消息中:
main.py
模块的第5行中包含"execution_count": null
,这会引发错误,因为显然您尚未定义名为null
的变量。 Perhaps you are confusing it with the builtin None
or meant to use the string "null"
(note the quotes), that's impossible to tell without the code. 也许您将其与内置的
None
混淆或打算使用字符串"null"
(请注意引号),如果没有代码,这是无法分辨的。 Your snippet above must be something else, it does not contain the problematic line. 您上面的代码段必须是其他代码,它不包含有问题的行。
If you downloaded the Jupyter Notebook file and saved it with a .py extension it doesn't only contain that script - it is only a small part of a larger .ipynb structure that the script is embedded within. 如果您下载了Jupyter Notebook文件并以.py扩展名保存,则它不仅包含该脚本-只是嵌入该脚本的较大.ipynb结构的一小部分。 Create a new file called main.py and copy the script into that file instead of downloading the notebook.
创建一个名为main.py的新文件,然后将脚本复制到该文件中,而不用下载笔记本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.