从CSV文件创建图形并使用Django和Pandas Python库渲染到浏览器 [英] Create a graph from a CSV file and render to browser with Django and the Pandas Python library

查看:168
本文介绍了从CSV文件创建图形并使用Django和Pandas Python库渲染到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在工作项目中使用Django框架,该框架将允许用户加载各种格式的文件(目前我仅处理CSV文件),并使用

I'm learning how to use the Django framework for a work project that will allow users to load files in various formats (at the moment I am only dealing with CSV files), graph that data using Pandas, and display that data back to the user via a Django template. I haven't had any problems creating the graph in iPython, but have been struggling with getting it to an HTML Django template.

我遵循了matplotlib中的以下示例:

I've followed the following example from matplotlib:

# graph input file

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter

def graph(request):
    fig = Figure()
    ax = fig.add_subplot(111)
    x = []
    y = []
    now = datetime.datetime.now()
    delta = datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now += delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas = FigureCanvas(fig)
    response = HttpResponse( content_type = 'image/png')
    canvas.print_png(response)
    return response

上面的示例很好用,我可以在模板中看到它,但这只是一个具有硬编码值的图形.

The above example works great and I can see it in a template, but that's just a graph with hard-coded values.

由于Pandas的语法看似过于简单,所以我尝试使用它,而我在Django中的尝试如下:

I've attempted to use Pandas because of its seemingly simplistic syntax and my attempts in Django are as follows:

# graph input file

import pandas as pd
from pandas import DataFrame

def graph(request):
    data_df = pd.read_csv("C:/Users/vut46744/Desktop/graphite_project/sampleCSV.csv")
    data_df = pd.DataFrame(dataArray)
    data_df.plot()
    response = HttpResponse( content_type = 'image/png')

    return response

在Django中,调用.plot()可以很好地显示图形,但是会显示HTML模板的空白页.我也尝试使用Numpy的genfromtxt()loadtxt(),但无济于事.另外,我的Google搜索也没有取得成果.

In Django calling the .plot() displays the graph fine, but displays a blank page to the HTML template. I've also tried using Numpy's genfromtxt() and loadtxt(), but to no avail. Also, my Google searches have not been fruitful either.

任何帮助或建议都会很棒.如果您知道熊猫可以提供更好的替代方法,那么我愿意尝试其他选择.

Any help or suggestion would be great. If you know of a better alternative to Pandas then I am willing to try other options.

推荐答案

还没有尝试过,但是我会像这样攻击它:

Haven't tried this yet, but I would attack it something like:

def graph(request):
    fig = Figure()
    ax = fig.add_subplot(111)
    data_df = pd.read_csv("C:/Users/vut46744/Desktop/graphite_project/sampleCSV.csv")
    data_df = pd.DataFrame(data_df)
    data_df.plot(ax=ax)
    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

这篇关于从CSV文件创建图形并使用Django和Pandas Python库渲染到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆