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.
what have you tried so far?
what results have you had?
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.
(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
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);
}
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?
iam getting this error
AttributeError: 'Dataset' object has no attribute 'html'