我正在尝试构建一个网络应用程序,它将清理 csv 并返回 pdf 格式的图表。
因为我是构建网络应用程序和烧瓶的新手。我尝试从构建一个简单的 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
这是我迄今为止用来构建网络应用程序的代码。我在 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.