简体   繁体   English

我可以将一个WTForm用于多个@ app.route吗?

[英]Can I use one WTForm for multiple @app.route's?

I need to access information from my WTForm in multiple @app.route's in my app.py , so that I can post data visualization for my app. 我需要从我的WTForm访问信息在多个@ app.route在我的app.py ,这样我就可以发布数据可视化我的应用程序。 Currently, I have a @app.route(/home/) page and user-entered text on this page is processed by a WTForm, and then passed to @app.route(/results/ ) where my code does some data analysis and then 1) displays some results and 2) saves some other information to JSON, which is to be used for D3 in its own @app.route(/visualization/) . 当前,我有一个@app.route(/home/)页面,该页面上的用户输入文本由WTForm处理,然后传递给@app.route(/results/ ),我的代码在其中进行一些数据分析并然后1)显示一些结果,2)将一些其他信息保存到JSON,该信息将在D3的@app.route(/visualization/)用于D3。 Because of complications with Javascript, I want to display my D3 visualization in an iframe of its own. 由于Javascript的复杂性,我想在自己的iframe中显示D3可视化。 Right now I can load the /home/ page, type in text and hit "Submit", which then redirects me to /results/ , and prints everything correctly except the iframe . 现在,我可以加载/home/页面,键入文本并单击“提交”,然后将我重定向到/results/ ,并正确打印除iframe之外的所有内容。 The problem is: I'm unable to get @app.route(/visualization/) to take information from my WTForm (in the same way that results is able to), so that the image can load the proper JSON file. 问题是:我无法获取@app.route(/visualization/)从WTForm中获取信息(以results能够使用的相同方式),以便图像可以加载正确的JSON文件。

Here's some of my code to better illustrate the problem. 这是我的一些代码,可以更好地说明问题。

app.py : app.py

# Home 
@app.route("/home/", methods=["GET", "POST"])
def gohome():
    error = None
    with open('somedata.pickle', 'rb')as f:
        some_content = pickle.load(f)
    try:
        if request.method == "POST":
            attempted_pmid = request.form['pmid']
    except Exception as e:
        #flash(e)
        return render_template("dashboard.html", error=error) 
    return render_template("dashboard.html", some_content=some_content)


# My WTForm for handling user-entered pmids
class pmidForm(Form):
    pmid = TextField('PubmedID')

# Results
@app.route("/results/", methods=["GET", "POST"])
def trying():
    form = pmidForm(secret_key='potato')
    try:
        if request.method == 'POST':
            entry = form.pmid.data #THIS IS THE USER INPUT FROM THE FORM #referencing 'class pmidForm'
            pmid_list = multiple_pmid_input(entry) #list for handling multiple pmids
            print(pmid_list)


            for user_input in pmid_list:
                print(str(user_input))
                user_input = str(user_input)
                # DO STUFF HERE #
                # SAVE A JSON FILE TO A FOLDER #

        return render_template('results.html')
    except Exception as e:
        return(str(e))


# Visualization
@app.route('/visualization/', methods=["GET", "POST"]) #for iframe
def visualization():
    #need to get last user_input
    form = pmidForm(secret_key='potato')
    try:
        if request.method == 'POST':
            entry = form.pmid.data 
            pmid_list = multiple_pmid_input(entry) 

            for user_input in pmid_list:
                print("This is the user input on /visualization/")
                print(str(user_input))
                user_input = str(user_input)

                #Load
                if user_input == pmid_list[-1]:
                    load_path = '/the/path/'+str(user_input)+'/'
                    completeName = os.path.join(load_path, ((str(user_input))+'.json'))
                    print(completeName)

                    with open(completeName, 'w') as load_data:
                        jsonDict = json.load(load_data)
                    print(jsonDict)
        return render_template('visualization.html', jsonDict=jsonDict)
    except Exception as e:
        return(str(e))

So as I have it now, home and results work fine together with the existing WTForm I have. 因此,就像我现在拥有的那样, homeresults与我现有的WTForm一起可以很好地工作。 I will do everything properly. 我会做的一切正确。 But in results.html I need to load visualization.html in an iframe like so: 但是在results.html我需要像这样在iframe加载visualization.html

Line in results.html : results.html一行:

<iframe  id="vis1" src="https://www.website.com/visualization/" width="1000" height="1000"></iframe>

With this configuration if I run my app.py , everything displays like normal except the iframe which displays: 使用此配置,如果我运行app.py ,则除显示以下内容的iframe之外,其他所有内容均会正常显示

local variable 'jsonDict' referenced before assignment 赋值之前引用了局部变量“ jsonDict”

Here, I assume that's in reference to my visualization.html which has the Jinja code: 在这里,我假设引用的是我的Jinjin代码的visualization.html

var myjson = {{ jsonDict|tojson }};

So obviously the @app.route(/visualization/) isn't getting the information from the WTForm as it should. 因此,很明显, @app.route(/visualization/)并没有从WTForm中获取信息。 How can I get this second @app.route to recognize the content in the WTForm like how it works with results ? 我如何才能获得第二个@app.route来识别@app.route中的内容,如其如何与results

Also, this may seem hacky, but I have very good reasons for putting my D3 in an iframe . 另外,这似乎有些骇人听闻,但我有充分的理由将D3放在iframe It's because I need to be able to toggle through multiple html's like /visualization / that each have complicated Javascript that conflict with each other. 这是因为我需要能够切换多个类似/visualization /的html,而每个html都有彼此冲突的复杂Javascript。 The best thing I can do is isolate them all in iframe 's. 我能做的最好的事情就是将它们全部隔离在iframe

The answer is no, you can't submit one form to multiple routes or share data between multiple routes. 答案是否定的,您不能将一种表单提交到多条路线或在多条路线之间共享数据。 My sollution to the problem of needing to share data between multiple routes was to create dynamic URLs. 对于需要在多个路由之间共享数据的问题,我的解决方法是创建动态URL。 So instead of always going to a results page, it would go to results/1234 and this way I was able to access the "1234" and use it in that html. 因此,与其始终访问results页面,不如转到results/1234 ,这样我就可以访问“ 1234”并在该html中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM