【发布时间】: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