需要借助 IPython.dispaly.HTML 对象,在 Jupyterlab 安装的时候就已经可用了,具体用法如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  from IPython.display import HTML
  import plotly.graph_objects as go

  # 准备数据
  x = [1, 2, 3, 4, 5]
  y = [1, 4, 9, 16, 25]

  # 创建线形图
  fig = go.Figure()
  fig.add_trace(go.Scatter(x=x, y=y, mode="lines"))
  fig.update_layout(
      title="Simple Line Plot",
      xaxis_title="X",
      yaxis_title="Y",
      font=dict(family="Courier New, monospace", size=18, color="#7f7f7f"),
  )


  # 显示图形
  HTML(fig.to_html())

/images/2024-06-04_17-20-28_screenshot.png