Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 show csv file in flask template.html
#1
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.
Quote
#2
what have you tried so far?
what results have you had?
Quote
#3
tablib is good as it can convert csv to html table.
Example email.csv:
Name,message,email
John Smith,Hello Mr. Smith,js@example.com
John Doe,Hello Mr. Doe,jd@example.com
Ms. Foo,Hello Ms. Foo,Foo@example.com
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.
Quote
#4
(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,js@example.com
John Doe,Hello Mr. Doe,jd@example.com
Ms. Foo,Hello Ms. Foo,Foo@example.com
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
Quote
#5
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);
  }
Quote
#6
(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
snippsat likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Sending file html ? JohnnyCoffee 3 174 Sep-06-2019, 04:32 PM
Last Post: snippsat
  [Flask] html error 405 SheeppOSU 0 255 Jun-08-2019, 04:42 PM
Last Post: SheeppOSU
  how i save the html form to flask database mebaysan 1 557 Feb-07-2019, 12:56 AM
Last Post: snippsat
  Flask generating a file for download darktitan 0 599 Dec-30-2018, 02:02 PM
Last Post: darktitan
  Passing a query value from a Bottle html template to a route with an encoding nikos 0 528 Sep-30-2018, 03:29 AM
Last Post: nikos
  Connecting python script into HTML with flask Piqurs 1 1,301 Sep-05-2018, 08:24 PM
Last Post: snippsat
  Reading a html file peterl 4 661 Aug-20-2018, 03:16 PM
Last Post: peterl
  [Flask] Uploading CSV file to flask, only first line being uploaded. Help ! KirkmanJ 2 2,221 Jun-25-2018, 02:24 PM
Last Post: KirkmanJ
  How to perform predictions on a data in csv file using Flask? manjusavanth 0 1,312 May-23-2018, 09:03 AM
Last Post: manjusavanth
  Problem parsing website html file thefpgarace 2 815 May-01-2018, 11:09 AM
Last Post: Standard_user

Forum Jump:


Users browsing this thread: 1 Guest(s)