如何在flask应用程序中显示数据表?

我正在尝试在我的烧瓶应用程序中显示数据表。 这是我的示例,仅显示iris数据集。

下面是我的app.py

from flask import flask,render_template
import seaborn as sns
import pandas as pd

iris = sns.load_dataset('iris')

app = flask(__name__)

@app.route('/')
def example():
    return render_template("example.html",value=iris)

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

这里是example.html

<!doctype html>
<html>
  <head></head>
  <body>   
    <table>
      <thead>
        <tr>
          <th>sepal_length</th>
          <th>sepal_width</th>
          <th>petal_length</th>
          <th>petal_width</th>
          <th>species</th>
        </tr>
      </thead>
      <tbody>
        {% for row in value %} 
          <tr>
            <td>{{row[0]}}</td>
            <td>{{row[1]}}</td>
            <td>{{row[2]}}</td>
            <td>{{row[3]}}</td>
            <td>{{row[4]}}</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </body>
</html>

我有这样的桌子:

sepal_length sepal_width petal_length petal_width   species
s   e   p   a   l
s   e   p   a   l
p   e   t   a   l
p   e   t   a   l
s   p   e   c   i

我花了几个小时试图找出问题所在。我仍然不知道为什么它不起作用。有人知道为什么会这样吗?

此外,flask中是否可以显示漂亮的数据表,而不是使用html编码?如果没有的话,我真的会感到惊讶。

mydisable 回答:如何在flask应用程序中显示数据表?


实际上,您将模板文件夹放在错误的路径中,这就是为什么您收到TemplateNotFound错误的原因。您需要将模板文件夹放置在“ os.path.abspath('templates')”位置,并且已经解决了这个问题。


app.py

从烧瓶导入烧瓶,render_template

将seaborn导入为sns

将熊猫作为pd导入

iris = sns.load_dataset('iris')

template_dir = os.path.abspath('templates')

static_dir = os.path.abspath('static')

app = Flask( __ name __ ,template_folder = template_dir,static_folder = static_dir,)

@ app.route('/')

def example():

public static void main(String[] args)
    {
        try
        {
            Main.call_me();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void call_me() throws Exception
    {
         MechanizeAgent agent = new MechanizeAgent();
        Document page = agent.get("https://campuscommune.tcs.com/en-in/intro");
        Form form = page.form("form");
        form.get("user[name]").set("<username>");
        form.get("user[password]").set("<password>");
        Resource response = form.submit();
        System.out.println(response.toString());

    }


如果 __ name __ == '__ main __'

    return render_template("example.html",value=iris)
本文链接:https://www.f2er.com/3162065.html

大家都在问