簡體   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