Python 重置渲染模板输出

Python 重置渲染模板输出,python,flask,jinja2,Python,Flask,Jinja2,我目前正在为battlefy网站制作一个解析器,用于快速提取结果并将其转换为wikicode 网站: 正在运行的it示例: 要分析的示例链接: 完整app.py: app.py代码段: @app.route("/", methods=["GET", "POST"]) @app.route("/lol", methods=["GET", "POST"]) @app.route(&

我目前正在为battlefy网站制作一个解析器,用于快速提取结果并将其转换为wikicode

网站:

正在运行的it示例:

要分析的示例链接:

完整app.py:

app.py代码段:

@app.route("/", methods=["GET", "POST"])
@app.route("/lol", methods=["GET", "POST"])
@app.route("/scraper", methods=["GET", "POST"])
def scraper():
    form = BattlefyForm()
    if form.validate_on_submit():
        url = form.matchhistory.data
        result = jloader(url)
        '''
        #result = url
        result = subprocess.check_output([sys.executable,
            "{}/stuff.py".format(directory), url]).decode('iso-8859-1')
        '''
        return render_template("scraper.html", form=form, result=result)
    return render_template("scraper.html", form=form)

每次我提交而不重新加载web应用程序时,新结果都会显示在旧结果中。是否有一个模块可用于清除会话缓存并重置网站状态,而无需每次提交新URL进行解析时都重新加载应用程序?

我不同意Akib的观点。无论是在收到表单数据后执行重定向,还是重新呈现整个页面并将其作为服务器的响应发送,都无关紧要。
每次调用后,无论是通过POST还是GET请求,都会重新加载并呈现整个页面。这可以通过使用ajax来减少。但是,这不会导致您的问题

问题不在路线内,而是在
stuff.py
模块内。
您可以使用全局变量将从加载的JSON文件提取的结果保存在列表中。
第一个解决方案是每次清空这些列表 调用jloader(url)。但这并不能完全解决问题。
如果多个用户同时启动呼叫,这将导致意外的错误结果。因为如果再次清空列表并填入进一步的结果,则无法完成上一个请求。
此外,应避免使用全局变量,只要它们不是绝对必要的。
由于列出的原因,我建议您不要使用全局变量和 将列表作为局部变量保存在函数中,每个 请求并在最后返回

这是一种方法,可以使代码更清晰,并且不使用全局变量。我只重写了你代码的摘录。我想你可以自己加上剩下的

import requests
from collections import namedtuple
from datetime import datetime
import gspread

class Match(namedtuple('Match', ['team_a', 'team_b', 'score_a', 'score_b'])):

    @property
    def winner(self):
        if self.score_a > self.score_b:
            return self.team_a
        elif self.score_a < self.score_b:
            return self.team_b
        return None

    def fmt(self):
        return '{{'\
            f'MatchSchedule|team1={self.team_a}|team2={self.team_b}'\
            f'|team1score={self.score_a}|team2score={self.score_b}'\
            f'|winner={self.winner}|date=...|time=...|timezone=PST|'\
            'dst=yes|vod1=|stream='\
            '}}'

def _get(data, key_path, default=None):
    tmp = data
    for k in key_path.split('.'):
        try:
            tmp = tmp[k]
        except KeyError:
            return default
    return tmp


def get_matches(url, team_alias={}):
    data = requests.get(url).json()

    for item in data:
        top_team = _get(item, 'top.name', _get(item, 'top.team.name'))
        low_team = _get(item, 'bottom.name', _get(item, 'bottom.team.name'))
        top_score = _get(item, 'top.score')
        low_score = _get(item, 'bottom.score')

        # Test for results that are None and react accordingly. 
        # The date and time queries are missing here. 

        top_team = team_alias.get(top_team, top_team)
        low_team = team_alias.get(low_team, low_team)

        match = Match(top_team, low_team, top_score, low_score)
        yield(match)

def get_teams():
    gc = gspread.service_account(filename='credentials.json')
    sh = gc.open_by_key('1N7wnIRWJRbULKychJU-EOyisuZBX1rgXwdW91Keki4M')

    col_name = worksheet.col_values(1)
    col_alias = worksheet.col_values(2)

    return dict(zip(col_name, col_alias))

def load_data(url):
    '''Use this instead of jloader(url).'''
    team_alias = get_teams()
    return '\n'.join(match.fmt() for match in get_matches(url, team_alias))

def main():
    url = 'https://dtmwra1jsgyb0.cloudfront.net/groups/5f60cffb30d29b119e36b42b/matches'
    dat = load_data(url)
    print(dat)

if __name__ == '__main__':
    main()
``
导入请求
从集合导入namedtuple
从日期时间导入日期时间
进口gspread
班级比赛(名称为双人('Match'、['team_a'、'team_b'、'score_a'、'score_b']):
@财产
def优胜者(自我):
如果self.score\u a>self.score\u b:
返回self.team_a
elif self.score_a
“每次我提交而不重新加载web应用程序时,”。我的印象是,默认情况下,表单提交会自动导致页面重新加载,不是吗?可能会吗?但无论如何,它似乎保持了相同的输出。也许我在python函数本身做了一些错误。您是否可能在
jloader(url)
中使用全局变量?@ThomasC也许我没有看到它,但我只是使用了您站点的url来搜索,返回的结果是完全正确的different@AkibRhast尝试你的链接,不刷新我看到的页面,因此,每次运行函数时,都会保存全局变量并将其追加,这导致了这些问题。我将研究您的代码并进行重构。