I have a CSV file called random.csv which I want to render on a html page if the user is logged in. I've tried using tablib for this.
__init__.py
from flask import Flask
import tablib
app = Flask(__name__)
dataset = tablib.Dataset()
with open(os.path.join(os.path.dirname(__file__), 'random.csv')) as f:
dataset.csv = f.read()
routes.py
@app.route('/dataset', methods=['GET', 'POST'])
@login_required
def dataset():
return dataset.html
This is the index.html file from where I want to link to the html page for the csv file.
{% extends "base.html" %}
{% block content %}
<p><a href="{{ url_for('dataset') }}">Click to see CSV</a> </p>
{% endblock %}
And this is the dataset.html file where I want to see the CSV data.
{% extends "base.html" %}
{% block content %}
{% endblock %}
I'm getting this error: AttributeError: 'function' object has no attribute 'html' The error is on the line in routes.py file where I return the dataset.html file.
I solved it by using pandas instead tablib. In my routes.py file just did this:
import pandas as pd
@app.route('/dataset', methods=['GET', 'POST'])
@login_required
def dataset():
table = pd.read_csv('filepath', encoding= 'unicode_escape')
return render_template("dataset.html", data=table.to_html())