In the script below, I try to call via FLASK several functions in my homepage in HTML format. Each function calculates a number of pending that I want to display in a table on my homepage. To do this, I have:
- computed with Panda a number of events to calculate from a csv file. I tested these functions separately and they work
- created a table on my HTML homepage with the different categories for which I would like to display the number of events from my csv file
- used the html_page.replace function to replace the number $$numberxx$$ in the html table by the result of my functions in Python
- for each function, I created a route indicating the HTML link and the concerned function so that Flask can understand that for this function, after having calculated the result, it is necessary to go on the homepage page and replace in the table the object $$number1$$ by the result1 or $$number2$$ by the result2
Unfortunately, when I run Flask, nothing is happening. I still have $$number1$$ or $$number2$$ on my webpage.
Could you please help me to correct my script below:
Python code:
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 code:
<!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 >
<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>
CodePudding user response:
This is not how you use Flask. Use render_template instead. Have a look at the doc first to understand the concept.
As an example, this function:
@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)
should look like:
@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)
And in the relevant HTML template, add a tag like this that will be replaced with appropriate values:
{{ result }}
Have a look at the doc, it's easy you'll see. If I may suggest, try to improve the naming of variables and function names: count_pending1/2 etc are all very similar and do not give any clue about the purpose of the function. The code should be more explicit - before you even look at the function it should be obvious what it is supposed to do.
When you need to review your code, it would help a lot to have meaningful names to immediately spot the relevant section you want to edit. You already have two functions that are named almost the same and are quite similar. Ask yourself if you really need two functions or even more. Perhaps a simple function with a conditional block would make more sense than repeating code and making the whole program longer than it could be.
