在django模板中显示django-pandas数据帧

时间:2020-12-11 20:22:01

I am trying to use django with pandas for data analysis. There seem to be no simple step by step tutorial on this. All the ones i have seen online just explain how to write the code in your django views.py file but none shows how to display the final product in the browser.

我正在尝试将django与pandas一起用于数据分析。似乎没有简单的分步教程。我在网上看到的所有内容都解释了如何在django views.py文件中编写代码,但没有一个显示如何在浏览器中显示最终产品。

Here is the code in my views.py

这是我的views.py中的代码

def index2(request):
    qs = Product.objects.all()
    df = read_frame(qs)
    html= df.to_html
    return HttpResponse(html)

but this does not work. Any detailed help will be appreciated. Please dont just point me to some documentation. In fact, most of django's documentation is not written in simple plain english --- it is even more confusing to some of us. Thank you.

但这不起作用。任何详细的帮助将不胜感激。请不要只指向我一些文档。事实上,django的大部分文档都不是用简单的英文写成的 - 这对我们中的一些人来说更加困惑。谢谢。

2 个解决方案

#1


7  

Here is a minimal but elegant solution using Django_Pandas and an 'extended Bootstrap table' (https://github.com/wenzhixin/bootstrap-table)

这是使用Django_Pandas和'扩展Bootstrap表'的最小但优雅的解决方案(https://github.com/wenzhixin/bootstrap-table)

The elegance comes from the ability to export a Pandas DataFrame to JSON, and for the Bootstrap Table script to consume that JSON content.

优雅来自于将Pandas DataFrame导出为JSON以及Bootstrap Table脚本使用该JSON内容的能力。

The HTML table is written for us, we don't need to worry about it (look below where we just include the 'table' tag without writing the rows ourselves, or even a for loop.) And it's interactive. And Bootstrap makes it look pretty.

HTML表是为我们编写的,我们不需要担心它(看下面我们只包含'table'标签,而不是自己编写行,甚至是for循环。)它是交互式的。而Bootstrap让它看起来很漂亮。

requirements: Bootstrap, JQuery, Django_Pandas, wenzhixin/bootstrap-table

要求:Bootstrap,JQuery,Django_Pandas,wenzhixin / bootstrap-table

models.py

models.py

from django.db import models
from django_pandas.managers import DataFrameManager

class Product(models.Model):
  product_name=models.TextField()
  objects = models.Manager()
  pdobjects = DataFrameManager()  # Pandas-Enabled Manager 

views.py

views.py

from models import Product
def ProductView(request):
  qs = Product.pdobjects.all()  # Use the Pandas Manager
  df = qs.to_dataframe()
  template = 'product.html'

  #Format the column headers for the Bootstrap table, they're just a list of field names, 
  #duplicated and turned into dicts like this: {'field': 'foo', 'title: 'foo'}
  columns = [{'field': f, 'title': f} for f in Product._Meta.fields]
  #Write the DataFrame to JSON (as easy as can be)
  json = df.to_json(orient='records')  # output just the records (no fieldnames) as a collection of tuples
  #Proceed to create your context object containing the columns and the data
  context = {
             'data': json,
             'columns': columns
            }
  #And render it!
  return render(request, template, context)

product.html

product.html

<script src='/path/to/bootstrap.js'>
<script src='/path/to/jquery.js'>
<script src='/path/to/bootstrap-table.js'>
<script src='/path/to/pandas_bootstrap_table.js'>
<table id='datatable'></table>
<!-- Yep, all you need is a properly identified
     but otherwise empty, table tag! -->

pandas_bootstrap_table.js

pandas_bootstrap_table.js

$(function() {
  $('#datatable')({
    striped: true,
    pagination: true,
    showColumns: true,
    showToggle: true,
    showExport: true,
    sortable: true,
    paginationVAlign: 'both',
    pageSize: 25,
    pageList: [10, 25, 50, 100, 'ALL'],
    columns: {{ columns|safe }},  // here is where we use the column content from our Django View
    data: {{ data|safe }}, // here is where we use the data content from our Django View. we escape the content with the safe tag so the raw JSON isn't shown.
  });
});

#2


1  

to_html is a function, you have to call it.

to_html是一个函数,你必须调用它。

def index2(request):
    df = read_frame(Product.objects.all())
    return HttpResponse(df.to_html())

#1


7  

Here is a minimal but elegant solution using Django_Pandas and an 'extended Bootstrap table' (https://github.com/wenzhixin/bootstrap-table)

这是使用Django_Pandas和'扩展Bootstrap表'的最小但优雅的解决方案(https://github.com/wenzhixin/bootstrap-table)

The elegance comes from the ability to export a Pandas DataFrame to JSON, and for the Bootstrap Table script to consume that JSON content.

优雅来自于将Pandas DataFrame导出为JSON以及Bootstrap Table脚本使用该JSON内容的能力。

The HTML table is written for us, we don't need to worry about it (look below where we just include the 'table' tag without writing the rows ourselves, or even a for loop.) And it's interactive. And Bootstrap makes it look pretty.

HTML表是为我们编写的,我们不需要担心它(看下面我们只包含'table'标签,而不是自己编写行,甚至是for循环。)它是交互式的。而Bootstrap让它看起来很漂亮。

requirements: Bootstrap, JQuery, Django_Pandas, wenzhixin/bootstrap-table

要求:Bootstrap,JQuery,Django_Pandas,wenzhixin / bootstrap-table

models.py

models.py

from django.db import models
from django_pandas.managers import DataFrameManager

class Product(models.Model):
  product_name=models.TextField()
  objects = models.Manager()
  pdobjects = DataFrameManager()  # Pandas-Enabled Manager 

views.py

views.py

from models import Product
def ProductView(request):
  qs = Product.pdobjects.all()  # Use the Pandas Manager
  df = qs.to_dataframe()
  template = 'product.html'

  #Format the column headers for the Bootstrap table, they're just a list of field names, 
  #duplicated and turned into dicts like this: {'field': 'foo', 'title: 'foo'}
  columns = [{'field': f, 'title': f} for f in Product._Meta.fields]
  #Write the DataFrame to JSON (as easy as can be)
  json = df.to_json(orient='records')  # output just the records (no fieldnames) as a collection of tuples
  #Proceed to create your context object containing the columns and the data
  context = {
             'data': json,
             'columns': columns
            }
  #And render it!
  return render(request, template, context)

product.html

product.html

<script src='/path/to/bootstrap.js'>
<script src='/path/to/jquery.js'>
<script src='/path/to/bootstrap-table.js'>
<script src='/path/to/pandas_bootstrap_table.js'>
<table id='datatable'></table>
<!-- Yep, all you need is a properly identified
     but otherwise empty, table tag! -->

pandas_bootstrap_table.js

pandas_bootstrap_table.js

$(function() {
  $('#datatable')({
    striped: true,
    pagination: true,
    showColumns: true,
    showToggle: true,
    showExport: true,
    sortable: true,
    paginationVAlign: 'both',
    pageSize: 25,
    pageList: [10, 25, 50, 100, 'ALL'],
    columns: {{ columns|safe }},  // here is where we use the column content from our Django View
    data: {{ data|safe }}, // here is where we use the data content from our Django View. we escape the content with the safe tag so the raw JSON isn't shown.
  });
});

#2


1  

to_html is a function, you have to call it.

to_html是一个函数,你必须调用它。

def index2(request):
    df = read_frame(Product.objects.all())
    return HttpResponse(df.to_html())