【问题标题】:FLASK : call several routes for several functions in one HTML pageFLASK:在一个 HTML 页面中为多个函数调用多个路由
【发布时间】:2022-01-30 15:42:19
【问题描述】:

在下面的脚本中,我尝试通过 FLASK 以 HTML 格式调用我主页中的几个函数。每个函数都会计算我想在主页上的表格中显示的待处理数量。为此,我有:

  • 用 Panda 计算了一些事件,以从 csv 文件中计算。我分别测试了这些功能,它们可以工作
  • 在我的 HTML 主页上创建了一个表格,其中包含我希望显示 csv 文件中的事件数量的不同类别
  • 使用 html_page.replace 函数将 html 表中的数字 $$numberxx$$ 替换为我在 Python 中的函数的结果
  • 对于每个函数,我都创建了一个路由,指明了 HTML 链接和相关函数,以便 Flask 可以理解,对于这个函数,在计算完结果后,需要去主页页面并在表格中替换对象 $$number1$$ 由 result1 或 $$number2$$ 由 result2

不幸的是,当我运行 Flask 时,什么都没有发生。我的网页上还有 $$number1$$ 或 $$number2$$。

您能帮我更正下面的脚本吗:

Python 代码:

import flask
import csv
import pandas as pd
import numpy as np
app = flask.Flask("app_monitoringissues")

def get_html(page_name):
html_file = open(page_name + ".html")
content = html_file.read()
html_file.close()
return content

@app.route("/homepage/<count_pending1>")
def count_pending1():
html_page = get_html ("homepage")
df = pd.read_csv("sortdata.csv")
count1 = len(df[df["Status_Issue"].astype(str).str.contains("Pending-to be checked")])
count2 = df["Status_Issue"].isna().sum()
result1 = count1 + count2
return html_page.replace("$$NUMBER1$$", str(result1))

@app.route("/homepage/<count_pending2>")
def count_pending2():
html_page = get_html ("homepage")
df = pd.read_csv("sortdata.csv")
count1 = len(df[df["Status_Issue"].astype(str).str.contains("Pending CP")])
result2 = count1
return html_page.replace("$$NUMBER2$$", result2)

HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Homepage</title>
</head>
<body>
<div class = "head">
<h1 id = "title homepage"> monitoring screen</h1>
<div class = "tablesupervisionarea">
    <table class="tablesupervision">
        <thead class ="theadsupervision">
            <tr>
                <th>Pending Supervision</th>
                <th> </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th>Need to be checked by Tax Regulatory</th>
                <td><ol>$$NUMBER1$$</ol></td>
            </tr>
            <tr>
                <th>Pending with CP</th>
                <td><ol>$$NUMBER2$$</ol></td>
            </tr>
            <tr>
        </tbody>
    </table>
</div>

【问题讨论】:

    标签: html flask replace routes call


    【解决方案1】:

    这不是你使用 Flask 的方式。请改用render_template。先看看doc 来理解这个概念。

    例如,这个函数:

    @app.route("/homepage/<count_pending2>")
    def count_pending2():
    html_page = get_html ("homepage")
    df = pd.read_csv("sortdata.csv")
    count1 = len(df[df["Status_Issue"].astype(str).str.contains("Pending CP")])
    result2 = count1
    return html_page.replace("$$NUMBER2$$", result2)
    

    应该看起来像:

    @app.route("/homepage/<count_pending2>")
    def count_pending2():
        df = pd.read_csv("sortdata.csv")
        result = len(df[df["Status_Issue"].astype(str).str.contains("Pending CP")])
        return render_template("homepage.html", result=result)
    

    在相关的 HTML 模板中,添加这样的标签,将替换为适当的值:

    {{ result }}
    

    看一下文档,你会很容易看到。 如果我可以建议,请尝试改进变量和函数名称的命名:count_pending1/2 等都非常相似,并且不提供有关函数用途的任何线索。代码应该更明确 - 在您查看函数之前,应该清楚它应该做什么。

    当您需要查看您的代码时,使用有意义的名称可以帮助您立即发现您要编辑的相关部分。您已经有两个名称几乎相同且非常相似的函数。问问自己,您是否真的需要两个或更多功能。也许一个带有条件块的简单函数比重复代码和使整个程序更长更有意义。

    【讨论】:

    • 感谢您的 cmets 和建议。它现在正在工作。
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多