How to display rows of csv file in django?
Asked Answered
A

2

8

I have a django app, which allows the user to upload a csv file, say a csv file of rankings of universities. I'd have to process the data that has been uploaded. For example, grey out any column which has string values and calculate the mean and std. deviation of all the values of a column. For this, I am using Pandas and converting the csv file to a pandas dataframe.

How do I display the dataset from the csv file using django? The column names cannot be hard coded because the user may upload any csv file. I checked django-tables2 and did the following

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
context = {'loaded_data': data}
return render(request, "dataflow/table.html", context)

but I get the error ValueError: Expected table or queryset, not DataFrame

Agrology answered 22/5, 2017 at 23:31 Comment(0)
H
15

Pandas dataframe can be converted to html table by itself. You can try

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
data_html = data.to_html()
context = {'loaded_data': data_html}
return render(request, "dataflow/table.html", context)

In the html page, use {{loaded_data | safe}} to render the table.

Harvison answered 30/5, 2017 at 21:53 Comment(0)
O
2

django-tables2 can't handle dataframes. You have do convert it into something django-tables2 understands, i.e.:

from django_tables2.tables import Table

in the view:

csvfile = request.FILES['csv_file']
data = pd.read_csv(csvfile.name)
df_table = Table(data.to_dict(orient='list'))
context = {'df_table': df_table}
return render(request, "dataflow/table.html", context)

in the template:

{% load render_table from django_tables2 %}
{% render_table df_table %}

This was a simple example, you might need do more work on the dataframe or even subclassing the Table class.

Ordovician answered 23/5, 2017 at 5:29 Comment(4)
I am getting AttributeError: 'DataFrame' object has no attribute 'as_dict'Agrology
my bad. the method is to_dict. Check the documentation to see the possible return shapes.Ordovician
Ok, that fixed it. But I still can't render the table. I have </div> {% render_table table_data %} <table class="table table-striped table-bordered table-hover table-sm table-responsive table-condensed table-fixed"> <thead> <tr>{% for item in table_data %} <td>{{ item }}</td> {% endfor %}</tr> and I get the error TypeError: 'Table' object is not iterableAgrology
if you are using django_tables2 render the table itself: {% render_table df_table %}. If you want more control over what is rendered, then you should think in a Form or something else.Ordovician

© 2022 - 2024 — McMap. All rights reserved.