plotly基本表未在web2py中显示

问题描述 投票:0回答:1

我对wep2py和plotly库有问题。我正在尝试在web2py中显示一个基本的绘图表。我已经在我的python控制器中使用plotly库制作了表格:

def test_table():
    fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
                                   cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
                          ])
    return fig.show()
    # fig.show() gives the same result

根据plotly docs,此代码应足以显示该图。我确实将其放在函数中,但我认为应该没有问题。

但是,当我使用参数将此功能传递给前端时,我的浏览器会尝试打开一个新选项卡,而运行我的web2py的选项卡上的结果仅显示为“ None”。

python plotly web2py plotly-python
1个回答
0
投票

执行return fig.show()会带来问题。当您在计算机上运行代码时,fig.show()确实会在浏览器中打开一个图,但它会返回None,因此它将无法在服务器上运行。

我建议保存一个静态HTML文件,然后可以提供该文件,或者可以让您的函数返回html字符串。希望对您有所帮助

import plotly.graph_objs as go
from plotly.offline import plot

fig1 = go.Figure(data=[{'type': 'bar', 'y': [1, 3, 2]}],
                        layout={'height': 400})

fig2 = go.Figure(data = [go.Table(header=dict(values=['A Scores', 'B Scores']),
                                       cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))])

# div1 = plot(fig1, output_type='file', include_plotlyjs=True, filename='table.html', auto_open=True)
div1 = plot(fig1, output_type='div', include_plotlyjs=False)
div2 = plot(fig2, output_type='div', include_plotlyjs=False)


html = """\
<html>
    <head>
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    </head>
    <body>
        {div1}
        {div2}
    </body>
</html>
""".format(div1=div1, div2=div2)

with open('multi_plot.html', 'w') as f:
    f.write(html)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.