此代码不起作用。
Python代码:
from datetime import datetime, timedelta
import os
from flask import Flask, render_template
#import numpy as np
import pandas as pd
TEMPLATE_DIR = os.path.abspath('../templates')
STATIC_DIR = os.path.abspath('../static')
app = Flask(__name__, template_folder=TEMPLATE_DIR, static_folder=STATIC_DIR)
# datetime object containing current date and time
day = datetime.now()
print(f"now ={day}")
nomi = ['Mario', 'Gianluca', 'Nicola', 'Andrea', 'Simone']
date = []
date.append('Nomi')
for i in range(40):
delta = timedelta(days=(i-(40/2)))
append_day = day+delta
displ_str = append_day.strftime("%d/%m/%Y")
print(displ_str)
date.append(displ_str)
df = pd.DataFrame(columns = date)
df['Nomi'] = nomi
@app.route('/', methods=("POST", "GET"))
def html_table():
# link_column is the column that I want to add a button to
return render_template("patient_list2.html", column_names=df.columns.values,
row_data=list(df.values.tolist()), link_column="Patient ID", zip=zip)
if __name__ == '__main__':
app.run(host='0.0.0.0')
HTML代码:
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document with python</title>
<link href="{{ url_for('static', filename='css/patient_list.css') }}"
rel="stylesheet" type="text/css" >
</head>
<body>
<table>
<tr>
{% for col in column_names %}
<th>{{col}}</th>
{% endfor %}
</tr>
{% for row in row_data %}
<tr>
{% for col, row_ in zip(column_names, row) %}
{% if col == link_column %}
<td>
<button type="submit" value={{ row_ }} name="person_id"
form="patient_form" class="patient_button">
{{ row_ }}
</button>
</td>
{% else %}
<td>{{row_}}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
CSS代码:
table{
display: flex;
border: 2px solid darkblue;
font-size: medium;
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
overflow-x: auto;
}
td, th {
border: 1px solid #121111;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #d6e0ee;
}
tr:first-child {
background-color: yellowgreen;
}
它返回以下错误:
[2024-08-20 12:00:15,876] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\showTable.py", line 35, in html_table
return render_template("patient_list2.html", column_names=df.columns.values, row_data=list(df.values.tolist()), link_column="Patient ID", zip=zip)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\templating.py", line 149, in render_template
template = app.jinja_env.get_or_select_template(template_name_or_list)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\jinja2\environment.py", line 1084, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\jinja2\environment.py", line 1013, in get_template
return self._load_template(name, globals)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\jinja2\environment.py", line 972, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\jinja2\loaders.py", line 126, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\templating.py", line 65, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\mnardo\Desktop\Pers\Flask_Prj\hello_flask\venv\lib\site-packages\flask\templating.py", line 99, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: patient_list2.html
127.0.0.1 - - [20/Aug/2024 12:00:15] "GET / HTTP/1.1" 500 -
您能否帮助我了解错误是什么?
问题是 TEMPLATE_DIR 和 STATIC_DIR 的值错误,修复它们后所有代码都可以正常工作。
正确的值为:
TEMPLATE_DIR = os.path.abspath('../Flask_Prj/hello_flask/templates')
STATIC_DIR = os.path.abspath('../Flask_Prj/hello_flask/templates/static')
谢谢你
马里奥