Embed Plotly Express in Web2PY

32 views
Skip to first unread message

Jitun John

unread,
Jan 25, 2024, 11:00:55 PMJan 25
to web2py-users
I would like to embed something on the following lines 

import plotly.express as px
fig = px.line(df_dataframe, x="datetime", y="load_avg_fifteen")


show it in html page similar to
<div id="ovc_cpu_load_chart" style="height: 520px"></div>

At present I am using HighCharts js, but as I totally work on pandas dataframes.. I imagine it is far more efficient and easier using plotly.
I am trying to avoid a heck lot of Javascript code that I am not good with.

Chart should still be interactive.

Al Ex

unread,
Jan 25, 2024, 11:56:04 PMJan 25
to web...@googlegroups.com, Jitun John
in controller
```
import plotly.express as px

def my_view():
    # Assuming df_dataframe is your Pandas DataFrame
    fig = px.line(df_dataframe, x="datetime", y="load_avg_fifteen")

    # Convert the figure to HTML
    plotly_chart = fig.to_html(full_html=False, include_plotlyjs='cdn')

    return dict(plotly_chart=plotly_chart)
```

in view

<!-- views/my_view.html -->
{{extend 'layout.html'}}

<div id="ovc_cpu_load_chart">
    {{=XML(plotly_chart)}}
</div>


It should work


On January 26, 2024 at 13:01:01, Jitun John (jitun...@gmail.com) wrote:

I would like to embed something on the following lines  import plotly.express as px fig = px.line(df_dataframe, x="datetime", y="load_avg_fifteen") show it in html page similar to
Reply all
Reply to author
Forward
0 new messages