我试图在不同的视图中使用数据框,因此我想将其设置为g变量。
到目前为止,我有
route = Blueprint('foo', __name__)
@route.before_request # or before_app_first_request
def set_dataframe():
g.df = None
@route.route('/', methods=['GET', 'POST'])
def base():
form = UploadFileForm()
if form.validate_on_submit():
df = pd.read_excel(form.file.data)
g.df = df
return redirect(url_for('foo.do_stuff'))
return render_template('index.html', form=form)
@route.route('/do-stuff-with-df', methods=['GET', 'POST'])
def do_stuff():
form = DoStuffForm()
df = g.df
choices = [(s, s.replace('_', ' ').title()) for s in df.columns]
# logic
return render_template('dostuff.html', form=form)
我得到AttributeError: 'NoneType' object has no attribute 'columns'
因此,我不确定为什么g.df = df
不能将我的g.df设置为数据帧吗?
我应该以这种方式使用g
吗?