Python 我的自定义视图未呈现,web2py未提供任何错误或票证

Python 我的自定义视图未呈现,web2py未提供任何错误或票证,python,matplotlib,web2py,Python,Matplotlib,Web2py,所以我想学习Web2Py。我看了几个示例,下载了几个示例,并使用了PHP框架CodeIgniter 我遇到的问题是,我创建的视图似乎不起作用。我正在尝试为我的业务数据构建度量仪表板。所以有很多时间向量图,相关图,饼图 我下载了这个示例,通过matplotlib在web2py中获取绘图。我的首选打包方式是: # -*- coding: utf-8 -*- import random, cStringIO try: from matplotlib.backends.backend_agg i

所以我想学习Web2Py。我看了几个示例,下载了几个示例,并使用了PHP框架CodeIgniter

我遇到的问题是,我创建的视图似乎不起作用。我正在尝试为我的业务数据构建度量仪表板。所以有很多时间向量图,相关图,饼图

我下载了这个示例,通过matplotlib在web2py中获取绘图。我的首选打包方式是:

# -*- coding: utf-8 -*-
import random, cStringIO
try:
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
except ImportError:
    raise HTTP(200,"requires matplotlib")

def plot(title='title',xlab='x',ylab='y',mode='plot',
         data={'xxx':[(0,0),(1,1),(1,2),(3,3)],
               'yyy':[(0,0,.2,.2),(2,1,0.2,0.2),(2,2,0.2,0.2),(3,3,0.2,0.3)]}):
    fig=Figure()
    fig.set_facecolor('white')
    ax=fig.add_subplot(111)
    if title: ax.set_title(title)
    if xlab: ax.set_xlabel(xlab)
    if ylab: ax.set_ylabel(ylab)
    legend=[]
    keys=sorted(data)
    for key in keys:
        stream = data[key]
        (x,y)=([],[])
        for point in stream:
            x.append(point[0])
            y.append(point[1])
        if mode=='plot':
            ell=ax.plot(x, y)
            legend.append((ell,key))
        if mode=='hist':
            ell=ax.hist(y,20)            
    if legend:
        ax.legend([x for (x,y) in legend], [y for (x,y) in legend], 
                  'upper right', shadow=True)
    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream)
    return stream.getvalue()

def pcolor2d(title='title',xlab='x',ylab='y',
             z=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]]):
    fig=Figure()
    fig.set_facecolor('white')
    ax=fig.add_subplot(111)
    if title: ax.set_title(title)
    if xlab: ax.set_xlabel(xlab)
    if ylab: ax.set_ylabel(ylab)
    image=ax.imshow(z)
    image.set_interpolation('bilinear')
    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream)
    return stream.getvalue()
此文件在模型中为plot.py。我在def plot上面添加了import部分,因为它丢失了。该部分来自Massimo在示例应用程序中的应用程序

然后,我在控制器中创建了这个文件plot_test.py:

# -*- coding: utf-8 -*-
# try something like
def dashboard():
    myplot()
    myhist()
    myplot2()

    img1 = URL('myplot')
    img2 = URL('myhist')
    img3 = URL('myplot2')

    return dict(img1=img1, img2=img2, img3=img3)

def test(): 
    """
    Attempt to plot something
    """

    output = DIV(myplot(), _style="float:left; width:40%;") + DIV(myhist(), _style="float:right; width:40%;") + '<br>' + myplot2()

    response.flash = "Why aren't you working?"
    return output # dict(message="hello from plot_test.py") # , img=IMG(src='http://www.web2pyslices.com/init/static/basic/images/Slice.png',alt='myplot'))


def myplot():
    response.headers['Content-Type']='image/png'
    return plot(data={'my plot':[(0,0),(1,1),(2,4),(3,9),(4,16)]})

def myhist():
    response.headers['Content-Type']='image/png'
    return plot(data={'my plot':[(0,0),(1,1),(2,4),(3,9),(4,16)]},mode='hist')

def myplot2():
    response.headers['Content-Type']='image/png'
    return pcolor2d(z=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7]])
该文件中有一些额外的代码,因为我已经尝试了几个变体,试图找到我缺少的一些小调整,以使其正常工作。有一个索引def,里面有一些敏感数据,我已经删除了,测试def只是一个测试,看看我是否可以让它工作

然后在视图中显示此仪表板绘图/测试/仪表板:

{{extend 'layout.html'}}
<h1>This is the plot_test/dashboard.html template</h1>
{{=BEAUTIFY(response._vars)}}

<div style="float:left; width:40%">
    <img src="{{=img1}}" alt="loading..." align="center"/>
</div>

<div style="float:right; width:40%">
    <img src="{{=img2}}" alt="loading..." align="center"/>
</div>

<br>

<div style="float:left; width:40%">
    <img src="{{=img3}}" alt="loading..." align="center"/>
</div>
转到../application\u name/plot\u test/dashboard只会返回一个损坏的图像图标。当我在Chrome中右键单击页面的空白部分时,页面源选项将变灰并禁用。没有错误消息,没有票证,没有数据

但是,新应用程序附带的通用应用程序页面仍然有效。此外,如果我使用../application\u name/plot\u test/myplot或myhist或myplot2,我会得到一个看起来正确的绘图。页面上没有任何其他内容,我也没有为它们创建视图,因此我假设这是绘图的通用视图

请注意,web2py运行在Ubunu 12.04机器上,该机器上以前没有安装任何Python。我正在通过Chrome从我的Windows box查看应用程序。我必须对db.py进行调整,才能让通用视图对我起作用

我的理解是,当我访问../application_name/plot_test/dashboard时,我应该收到plot_test/dashboard.html视图,其中包含控制器中plot_test.py文件中仪表板def提供的数据。显然,plot_test.py在模型中查找plot.py没有问题,因为当我单独转到绘图页面时,它可以生成绘图

即使我对URL的使用。。。是不正确的,我希望仪表板视图显示提供的标题、文本和3个包含损坏图像图标的div。然后,当我单击查看源代码时,如果URL不正确,我可以看到URL指向的位置


我确信我错过了一些非常简单的东西,但我被难住了。

问题是您的仪表板函数进行了以下调用:

myplot()
myhist()
myplot2()

这些函数中的每一个都将内容类型头设置为image/png,因此浏览器希望使用png图像而不是HTML。没有理由在dashboard函数中调用这些函数,所以只需删除这些行。

我通过web界面创建了dashboard视图,该界面似乎自动添加了.html。我没有明确地键入它。但是,在您的建议之后,我从dashboard复制了代码,将其删除,并创建了一个新的plot_test/dashboard.html,其中.html显式包含在定义中。然后我将代码粘贴到新文件中。和以前一样的结果,这是有道理的。但是,我正在尝试在仪表板视图中查看这些绘图的结果。是否应在删除response.headers行的情况下调用这些函数?还是从控制器中移除标题?或者??我在response.header行前面放了一个if语句,现在仪表板显示了,但是图像没有…谢谢你。。。明白了,只需留下myplot、myhist和myplot2,不带任何if语句。不要从控制器中调用它们,只通过IMG标签从视图中调用它们。是的,尽管从技术上讲,您不是从视图中调用这些函数。相反,您正在为IMG标记指定源URL,这最终导致在浏览器请求这些URL时在控制器中调用这些函数。