Python Forum
show csv file in flask template.html - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: show csv file in flask template.html (/thread-6219.html)



show csv file in flask template.html - rr28rizal - Nov-11-2017

Hello, I have a question.
I have python script that collect tweets from twitter and store it csv file.
how to show the csv file in index.html?
I use flask as framework.


RE: show csv file in flask template.html - Larz60+ - Nov-11-2017

what have you tried so far?
what results have you had?


RE: show csv file in flask template.html - snippsat - Nov-11-2017

tablib is good as it can convert csv to html table.
Example email.csv:
Name,message,email
John Smith,Hello Mr. Smith,[email protected]
John Doe,Hello Mr. Doe,[email protected]
Ms. Foo,Hello Ms. Foo,[email protected]
from flask import Flask, render_template
import tablib
import os

app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'email.csv')) as f:
    dataset.csv = f.read()

@app.route("/")
def index():    
    return dataset.html    

if __name__ == "__main__":
    app.run()
Will display a html table when open browser,example output CodePen.

To get more control like adjusting CSS.
Can send htlm code into index.html with jinja.
I have written a test with this,can post it if interested.


RE: show csv file in flask template.html - rr28rizal - Nov-12-2017

(Nov-11-2017, 12:15 PM)Larz60+ Wrote: what have you tried so far?
what results have you had?

I tried pandas and use this code
df = pd.read_csv("file.csv")
        df.to_html("detail.html")
but it just created a new html page, that also didn't really work cause I can't open it in flask server.
What I wanted to do is to show the csv table in index.html template.

(Nov-11-2017, 12:27 PM)snippsat Wrote: tablib is good as it can convert csv to html table.
Example email.csv:
Name,message,email
John Smith,Hello Mr. Smith,[email protected]
John Doe,Hello Mr. Doe,[email protected]
Ms. Foo,Hello Ms. Foo,[email protected]
from flask import Flask, render_template
import tablib
import os

app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'email.csv')) as f:
    dataset.csv = f.read()

@app.route("/")
def index():    
    return dataset.html    

if __name__ == "__main__":
    app.run()
Will display a html table when open browser,example output CodePen.

To get more control like adjusting CSS.
Can send htlm code into index.html with jinja.
I have written a test with this,can post it if interested.

how to send the "dataset.html" content to "index.html"??
I don't know how to use jinja, I'm really new to python programming


RE: show csv file in flask template.html - snippsat - Nov-12-2017

Quote:but it just created a new html page, that also didn't really work cause I can't open it in flask server.
What I wanted to do is to show the csv table in index.html template
The option here is to use Jupyter Notebook,
Notebooks can be shared for free without using a server examples here.
 
Quote:how to send the "dataset.html" content to "index.html"??
I don't know how to use jinja, I'm really new to python programming
Jinja is build into Flask,as it's made bye same author.
It's a way for getting stuff from a Flask server and out to client side(browser).

Here is the setup,
now have control over where in page it should be and size/color and more though CSS.

Folder setup:
tab_test\
  |-- app.py
  |-- email.csv
  templates\
    |-- index.html
  static\
    css\
      |-- style.css
app.py:
from flask import Flask, render_template
import tablib
import os

app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'email.csv')) as f:
    dataset.csv = f.read()

@app.route("/")
def index():
    data = dataset.html
    #return dataset.html
    return render_template('index.html', data=data)

if __name__ == "__main__":
    app.run()
index.html:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}"/>
  <title>Show CSV</title>
</head>
<body>
  <div class="table">
    {% block body %}
    {{ data|safe }}
    {% endblock %}  
  </div>
</body>
</html>
style.css:
.table {       
  position: absolute;
  font-size: 150%;
  width: 480px;
  height: 200px;
  z-index: 15;
  top: 20%;
  left: 50%;
  margin: -100px 0 0 -150px;
  background: rgb(236, 240, 204);
  }



RE: show csv file in flask template.html - rr28rizal - Nov-12-2017

(Nov-12-2017, 11:11 AM)snippsat Wrote:
Quote:but it just created a new html page, that also didn't really work cause I can't open it in flask server.
What I wanted to do is to show the csv table in index.html template
The option here is to use Jupyter Notebook,
Notebooks can be shared for free without using a server examples here.
 
Quote:how to send the "dataset.html" content to "index.html"??
I don't know how to use jinja, I'm really new to python programming
Jinja is build into Flask,as it's made bye same author.
It's a way for getting stuff from a Flask server and out to client side(browser).

Here is the setup,
now have control over where in page it should be and size/color and more though CSS.

Folder setup:
tab_test\
  |-- app.py
  |-- email.csv
  templates\
    |-- index.html
  static\
    css\
      |-- style.css
app.py:
from flask import Flask, render_template
import tablib
import os

app = Flask (__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__),'email.csv')) as f:
    dataset.csv = f.read()

@app.route("/")
def index():
    data = dataset.html
    #return dataset.html
    return render_template('index.html', data=data)

if __name__ == "__main__":
    app.run()
index.html:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <link rel=stylesheet type=text/css href="{{ url_for('static', filename='css/style.css') }}"/>
  <title>Show CSV</title>
</head>
<body>
  <div class="table">
    {% block body %}
    {{ data|safe }}
    {% endblock %}  
  </div>
</body>
</html>
style.css:
.table {       
  position: absolute;
  font-size: 150%;
  width: 480px;
  height: 200px;
  z-index: 15;
  top: 20%;
  left: 50%;
  margin: -100px 0 0 -150px;
  background: rgb(236, 240, 204);
  }

hey, it works.
Thank you so much. Smile



RE: show csv file in flask template.html - morph10 - Jul-29-2020

Hello I have the same kind of issue. I am trying to open a CSV and display it in html. I have done the above code leading it to my csv. The problem is it just returns none on a blank screen.
Now if I remove html from data = dataset.html (changed to -'data = dataset)it displays my CSV on screen, but I cannot do anything with it and it is not in table format.
What do you think could be wrong?


RE: show csv file in flask template.html - humanz - Nov-09-2020

iam getting this error
AttributeError: 'Dataset' object has no attribute 'html'


RE: show csv file in flask template.html - adamabusamra - Apr-12-2021

(Nov-09-2020, 01:32 PM)humanz Wrote: iam getting this error
AttributeError: 'Dataset' object has no attribute 'html'

In the documentation it says:
Quote:"This format is optional, install Tablib with pip install "tablib[html]" to make the
format available."

It worked for me.

here's the reference: https://tablib.readthedocs.io/en/stable/formats/#html