我正在开发一个小的web应用程序来创建一些图表,我需要创建一个变量来为每个web应用程序会话保存一个唯一的文件名,这样用户在将图表保存为pdf时不会得到错误的文件。为此,我使用classful将相关视图包装到一个类中,并创建了一个实例变量来保存文件名。你知道吗
class PiperView(FlaskView):
route_base = '/'
def __init__(self):
self.piper_name = '_init_.pdf'
self.tst_index = 0
self.tst_plot = 0
self.tst_download = 0
self.tst_master = 0
@route('/',methods=['GET','POST'])
@route('/index/',methods=['GET','POST'],endpoint='index')
@nocache
def index(self):
self.piper_name = '_piper.pdf'
#test Code
#=======================================================================
file = open(fpath+'index.txt','a')
self.tst_index += 1
self.tst_master += 1
file.write(str(self.tst_index)+"."+str(self.tst_master)+") " +str(self.piper_name)+', ')
file.close()
#=======================================================================
plot_data = np.loadtxt('piper_data.csv', delimiter=',', skiprows=1 )
html_plot = Markup(piper(plot_data, ' ', alphalevel=1.0, color=False, file_nam=self.piper_name))
return render_template('plot.html',title='The Plot', figure=html_plot)
@route('/plot',methods=['GET','POST'],endpoint='plot')
@nocache
def plot(self):
self.piper_name = str(random.randint(0,10000001))+'_piper.pdf'
#test Code
#=======================================================================
file = open(fpath+'plot.txt','a')
self.tst_plot += 1
self.tst_master += 1
file.write(str(self.tst_plot)+"."+str(self.tst_master)+" ) " +str(self.piper_name)+', ')
file.close()
#=======================================================================
try:
f = request.files['data_file']
plot_data = np.loadtxt(f, delimiter=',', skiprows=1 )
html_plot = Markup(piper( plot_data, ' ', alphalevel=1.0, color=False, file_nam=self.piper_name))
return render_template('plot.html',title='The Plot', figure=html_plot)
except:
return render_template('plot.html',title='The Plot', figure="There Seems To Be A Problem With Your Data")
@route('/download',methods=['GET','POST'],endpoint='download')
@nocache
def download(self):
#test Code
#=======================================================================
file = open(fpath+'download.txt','a')
self.tst_download += 1
self.tst_master += 1
file.write(str(self.tst_download)+"."+str(self.tst_master)+") " +str(self.piper_name)+', ')
file.close()
#=======================================================================
return send_from_directory(directory=fpath,filename=self.piper_name)
问题是保存文件名的实例变量不能在方法之间共享。我添加了一些测试代码来尝试找出发生了什么。'tst\u index'、'tst\u plot'和'tst\u download'的每一个行为都与预期的一样,它们都会递增,但是'tst\u master'在方法调用之间不会递增。你知道吗
测试代码的输出是:
你知道吗索引.txt
1.1) _派珀.pdf,
你知道吗绘图.txt
1.1 ) 7930484_派珀.pdf, 2.2 ) 9579691_派珀.pdf哦
你知道吗下载.txt
1.1)init.pdf,2.2)init.pdf
当我调用索引视图一(1)次、绘图视图两(2)次和下载视图(2)次时。如您所见,'tst\u master'实例变量在方法调用之间没有得到更新。你知道吗
我知道这在我测试的普通python中是可行的,但是我缺少什么关于flask和flask\u classful的东西导致了这种情况?你知道吗
在应用程序中嵌入这样的状态通常是个坏主意。无法保证生成响应的视图实例将持续处理多个请求。将数据存储在服务器端之外的数据库、键值存储甚至磁盘上的某个位置的文件,或者浏览器的客户端。Flask有许多插件使之更容易(
flask-sqlalchemy
、flask-session
、flask-redis
,等等)。你知道吗Flask本机提供} 如果您关心的是在服务器端存储东西,那么它可能会做您想做的事情,而不需要太多额外的开销。只要用文件系统会话接口配置它,就会得到一个
flask.session
对象,该对象将信息存储在客户端的cookie中。^{flask.session
变量,它处理将用户请求链接到文件系统上存储的数据的所有神奇功能。你知道吗你的任务太复杂了。你可能不需要使用flask classful。你知道吗
你可以使用普通的烧瓶会话。会话对于每个用户都是唯一的。唯一需要做的就是为每个文件使用一些唯一的ID。如果您的用户登录到您的web应用并且其凭据存储在数据库中,则此文件id可以是用户id。或者您可以随机生成此文件id。然后您可以将文件名存储在flask会话中,如下所示:
希望这有帮助。你知道吗
相关问题 更多 >
编程相关推荐