尝试运行Flask Web应用时获取未定义的错误为null [英] Getting a null is not defined error when trying to run flask web app

查看:53
本文介绍了尝试运行Flask Web应用时获取未定义的错误为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to build a web app that will clean up a csv and return graphs in pdf format.

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

Since I am new to building web apps and flask in general. I am trying to start off by building a simple web app that allows users to upload files of csv format. However, when I try to run the app on bash I get this error message:

(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上运行此文件

Here is the code I have used to build the web app I have thus far. I wrote this code in Jupyter, downloaded it as a .py file and tried running this file on 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.

编辑在破折号上运行命令ipython main.py返回此:

Edit running the command ipython main.py on dash returned this:

/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.

推荐答案

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

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. Create a new file called main.py and copy the script into that file instead of downloading the notebook.

这篇关于尝试运行Flask Web应用时获取未定义的错误为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆