1

我有以下代码(在烧瓶视图中)显示 12 个月内不同餐厅的平均评分:

@bp.route("/")
@bp.route("/index")
def index():
  ratings = {
    "restaurant_1": [3.6, 3.2, 4.1, 3.9, 4.6, 4.4, 5, 4.8, 4.2, 3.7, 4.3, 3],
    "restaurant_2": [4.7, 4, 4.1, 2.1, 2.8, 3.5, 5, 4.1, 4.2, 2, 4, 1],
    "restaurant_3": [3, 2, 1.1, 1.5, 1.7, 3, 2, 1, 1.7, 2, 1, 1.8],
    "restaurant_4": [1, 1.5, 2.1, 1.7, 1.4, 1, 1.1, 1.3, 1.3, 1.5, 2, 1.8]
  }

  basic_plot = px.line(
    x=["January", "February", "March", "April", "May", "June", "July", "August", "September",
       "October", "November", "December"],
    y=[ratings["restaurant_1"], ratings["restaurant_2"], ratings["restaurant_3"], ratings["restaurant_4"]],
    title="Brands Rating", template="plotly_dark")

  basic_plot.update_layout({
    "title": "Ratings evolution",
    "xaxis_title": "Months",
    "yaxis_title": "Average rating",
    "legend_title": "Brands"
  })

  i = 0
  for restaurant in ratings:
    basic_plot["data"][i]["name"] = "Restaurant {}".format(i+1)

    basic_plot["data"][i]["hovertemplate"] = "{}={}<br>{}=%{{x}}<br>{}=%{{y}}<extra></extra>".format(
      "Month",
      basic_plot["data"][i]["name"],
      "Brand",
      "Average rating"
    )
    i +=1

  graph = basic_plot.to_html(full_html=False)
  return render_template("index.html", graph=graph)

这呈现了这一点: 在此处输入图像描述

由于我将拥有更多餐厅,因此我想添加一个下拉过滤器,以便能够选择 1 家或更多餐厅(默认情况下仅选择 1 家)并根据选择自动更新图表。这几个月也一样,因为我将获得更大的时间框架。

我找到了一些 Plotly 的示例,但没有找到 Plotly Express 的示例。请注意,我使用的不是 Pandas,而是从 PSQL 数据库加载的 Flask 模型的列表/字典。

谢谢您的帮助

4

0 回答 0