【问题标题】:How can I add python functions into HTML? [duplicate]如何将 python 函数添加到 HTML 中? [复制]
【发布时间】:2021-08-18 16:11:37
【问题描述】:

我正在使用 Python 烧瓶编写一个网站。它必须读取一个 csv 文件(保存为 csv 的 excel 文件),在那里进行一些计算,并且我需要在网站上显示实际的表格和结果。如何在 html 文件中编写函数?我需要把它们写在“一些桌子”的地方。 这是我的 html 文件:

<!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>

这是我的 py 文件

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__)

with open('testfinal.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    print(csv_reader)

@app.route('/')
def about():
    return render_template("about.html")

if __name__ == "__main__":
    app.run(debug=True)

【问题讨论】:

    标签: python html css flask web


    【解决方案1】:

    您可以将变量从烧瓶传递到您的模板:

     <!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>
    

    然后在你的服务器(python)中:

    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)
    

    注意:python 烧瓶中的 some_table= 应该与 html 文件中的 {{ some_table }} 匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多