如何将python函数添加到HTML中?
我正在用Python编写一个网站。它必须读取csv文件(保存为csv的excel文件),在那里进行一些计算,我需要在网站上显示实际的表格和结果。如何在html文件中编写函数?我需要把它们写在写着“某张桌子”的地方。 这是我的html文件:如何将python函数添加到HTML中?,python,html,css,flask,web,Python,Html,Css,Flask,Web,我正在用Python编写一个网站。它必须读取csv文件(保存为csv的excel文件),在那里进行一些计算,我需要在网站上显示实际的表格和结果。如何在html文件中编写函数?我需要把它们写在写着“某张桌子”的地方。 这是我的html文件: <!doctype html> <html lang="ru"> <link rel="stylesheet" href="{{ url_for('static', filenam
<!doctype html>
<html lang="ru">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container">
<nav class="navbar">
<ul>
<li><a href="#table">first</a></li>
<li><a href="#problems">second</a></li>
<li><a href="#rank">third</a></li>
<li><a href="#cheated">forth</a></li>
</ul>
</nav>
<section id="table">
<h1>1</h1>
<p class="lead"> some table</p>
</section>
<section id="problems">
<h1>2</h1>
<p class="lead">some table</p>
</section>
<section id="rank">
<h1>3</h1>
<p class="lead">some table</p>
</section>
<section id="cheated">
<h1>4</h1>
<p class="lead">some table</p>
</section>
</div>
</html>
您可以将变量从flask传递到模板:
<!doctype html>
<html lang="ru">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container">
<nav class="navbar">
<ul>
<li><a href="#table">first</a></li>
<li><a href="#problems">second</a></li>
<li><a href="#rank">third</a></li>
<li><a href="#cheated">forth</a></li>
</ul>
</nav>
<section id="table">
<h1>1</h1>
<p class="lead">{{ some_table }} some table</p>
</section>
<section id="problems">
<h1>2</h1>
<p class="lead"> {{ some_table }} some table</p>
</section>
<section id="rank">
<h1>3</h1>
<p class="lead">{{ some_table }} some table</p>
</section>
<section id="cheated">
<h1>4</h1>
<p class="lead"> {{ some_table }}some table</p>
</section>
</div>
</html>
注意:some_table=
在python flask中应该与html文件中的{{some_table}}
匹配
<!doctype html>
<html lang="ru">
<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">
<div class="container">
<nav class="navbar">
<ul>
<li><a href="#table">first</a></li>
<li><a href="#problems">second</a></li>
<li><a href="#rank">third</a></li>
<li><a href="#cheated">forth</a></li>
</ul>
</nav>
<section id="table">
<h1>1</h1>
<p class="lead">{{ some_table }} some table</p>
</section>
<section id="problems">
<h1>2</h1>
<p class="lead"> {{ some_table }} some table</p>
</section>
<section id="rank">
<h1>3</h1>
<p class="lead">{{ some_table }} some table</p>
</section>
<section id="cheated">
<h1>4</h1>
<p class="lead"> {{ some_table }}some table</p>
</section>
</div>
</html>
from flask import Flask, render_template
from flask import url_for
from flask import make_response, request, redirect
import io
import csv
from io import TextIOWrapper
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
@app.route('/')
def about():
with open('testfinal.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
print(csv_reader)
return render_template("about.html", some_table=csv_reader)
if __name__ == "__main__":
app.run(debug=True)