Python Plotly:创建一个带有下拉菜单的绘图直方图图,然后将其放入HTML中

发布于 2025-01-26 14:35:42 字数 2065 浏览 3 评论 0原文

目标:使用下拉菜单创建一个模块直方图,然后将其放入HTML中。 HTML将具有多种不同类型的图形(在此问题中未提及)。

我实际上具有许多列的大数据框/CSV。但是对于这个问题,我正在考虑一个非常简单的CSV/DataFrame。 CSV/DataFrame有三列 - 主机,请求,事件。以下是样本CSV。

HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning

目前,我正在为每个主机绘制“请求与事件”的单独直方图图,然后从中创建一个HTML。意味着,如果有四个不同的主机,那么我正在绘制HTML中的四个不同的直方图图。 以下是我的代码。

import pandas as pd
import plotly.express as px

print(f"START")
df = pd.read_csv("dropdown.csv")
hosts = list(df['HOST'].unique())
print(hosts)
for host in hosts:
    title = "Dropdown grap for host = " + host
    df1 = df.loc[(df['HOST'] == host)]
    graph = px.histogram(df1, x='REQUEST', color='INCIDENT', title=title)
    with open("dropdown.html", 'a') as f:
        f.write(graph.to_html(full_html=False, include_plotlyjs=True))
print(f"END")

以下是我的输出html有四个图

但是我的目标是在输出HTML中绘制单个直方图图,主机是下拉列表。我应该能够从下拉菜单中选择不同的主机,以获取每个相应主机的图形。 使用Plotly Express,我没有任何选择来实现所需的输出。需要帮助。 ,特别是如果我可以使用情节实现这一目标。表达本身会很棒! 也欢迎其他选项。

Objective: Create a plotly HISTOGRAM with a dropdown menu and put it in a html. The html will have multiple different types of graphs (not mentioned in this question).

I actually have a large dataframe/csv with many columns; but for this question I'm considering a very SIMPLE csv/dataframe.
The csv/dataframe has three columns - HOST,REQUEST,INCIDENT. Below is the sample csv.

HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning

Currently I'm plottting separate HISTOGRAM graphs for 'REQUEST Vs INCIDENT' for each HOST and then creating a html out of it. Means if there're four different hosts, then I'm plotting four different HISTOGRAM graphs in my html.
Below is my code.

import pandas as pd
import plotly.express as px

print(f"START")
df = pd.read_csv("dropdown.csv")
hosts = list(df['HOST'].unique())
print(hosts)
for host in hosts:
    title = "Dropdown grap for host = " + host
    df1 = df.loc[(df['HOST'] == host)]
    graph = px.histogram(df1, x='REQUEST', color='INCIDENT', title=title)
    with open("dropdown.html", 'a') as f:
        f.write(graph.to_html(full_html=False, include_plotlyjs=True))
print(f"END")

Below is my output html having four graphs
enter image description here
enter image description here
enter image description here
enter image description here

But My Objective is to plot a single HISTOGRAM graph in my output html, with HOST being the dropdown. I should be able to select different HOSTs from the dropdown to get graph for each respective HOST.
Using plotly express I'm NOT getting any option to achieve my required output. Need help with this. Especially if I can achieve this using plotly.express itself that'll be great!
Other options are also welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2025-02-02 14:35:42

您可以循环遍历所有可能的主机,并使用fig = px.histogram(df_subset_by_host,x ='request',color ='iscod'),然后提取存储在中的x数组数据中图。_DATA对象,并将此数据分配给每个主机选择按钮的“ X” arg。

例如:

from io import StringIO
import pandas as pd
import plotly.express as px

data_str = StringIO("""HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning""")

df = pd.read_csv(data_str)

hosts = list(df['HOST'].unique())
host_default = "host1"
title = f"Dropdown grap for host = {host_default}"

fig = px.histogram(df.loc[df['HOST'] == host_default], x='REQUEST', color='INCIDENT', title=title)

buttons = []
for host in hosts:
    df_host = df.loc[(df['HOST'] == host)]
    fig_host = px.histogram(df_host, x='REQUEST', color='INCIDENT')
    buttons.append(
        dict(
            label=host,
            method="update",
            args=[
                {
                    "x": [trace['x'] for trace in fig_host._data],
                    "title": f"Dropdown group for host {host}"
                }
            ]
        )
    )

fig.update_layout(
    updatemenus=[
        dict(
            type="dropdown",
            direction="down",
            showactive=True,
            buttons=buttons
        )
    ]
)

fig.show()

“在此处输入图像描述”

You can loop through all possible hosts, and create a corresponding fig using fig = px.histogram(df_subset_by_host, x='REQUEST', color='INCIDENT'), then extract the x array data stored in the fig._data object, and assign this data to the "x" arg of each host selection button.

For example:

from io import StringIO
import pandas as pd
import plotly.express as px

data_str = StringIO("""HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning""")

df = pd.read_csv(data_str)

hosts = list(df['HOST'].unique())
host_default = "host1"
title = f"Dropdown grap for host = {host_default}"

fig = px.histogram(df.loc[df['HOST'] == host_default], x='REQUEST', color='INCIDENT', title=title)

buttons = []
for host in hosts:
    df_host = df.loc[(df['HOST'] == host)]
    fig_host = px.histogram(df_host, x='REQUEST', color='INCIDENT')
    buttons.append(
        dict(
            label=host,
            method="update",
            args=[
                {
                    "x": [trace['x'] for trace in fig_host._data],
                    "title": f"Dropdown group for host {host}"
                }
            ]
        )
    )

fig.update_layout(
    updatemenus=[
        dict(
            type="dropdown",
            direction="down",
            showactive=True,
            buttons=buttons
        )
    ]
)

fig.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文