当 Altair Chart 是 JupyterLab 单元格中的最后一条语句时,是什么导致显示它?
What causes an Altair Chart to be displayed when it is the last statement in a JupyterLab cell?
我有一个 class 想在 JupyterLab 中使用。当 JupyterLab 笔记本中的最后一条语句计算出我的 class 的一个实例时,我想显示一个 Altair Chart
代表我的 class 的对象。推荐的方法是什么?在下面的示例中,似乎在这种情况下我的 class 上调用了 repr
函数,所以这是一个钩子,我可以用它来控制当 JupyterLab 笔记本中的最后一条语句计算时发生的事情到我的 class 的一个实例。但是,我不确定下一步该怎么做。我可以在代表我的对象的 Altair Chart
上调用 display
,然后 return 一个空字符串。但是,return 是空字符串而不是更有意义的字符串似乎有点奇怪。例如,如果我想在其他上下文中使用 repr
来显示对象的表示,那么我将无法做到。当我在 Altair Chart
上调用 repr
时,我得到一个类似于 alt.Chart(...)
的字符串,因此似乎必须有其他方法来控制 JupyterLab 单元格中的最后一条语句时发生的情况是一些对象。有更好的方法吗?
import altair as alt
import numpy as np
import pandas as pd
class RandomWalk:
def __init__(self, n):
self._data = pd.DataFrame({
'x': np.arange(n) + 1,
'y': np.cumsum(np.random.normal(size=n)),
})
self._chart = alt.Chart(self._data).mark_line().encode(x='x', y='y')
def __repr__(self):
# When I use the following line for the body of __repr__, then
# JupyterLab prints the string 'alt.Chart(...)' and does not display
# the chart.
# return repr(self._chart)
# The following line displays the chart as desired, but it feels odd to
# then also return the empty string.
self._chart.display()
return ''
RandomWalk(100)
Altair 图表通过 Jupyter 的 _repr_mimebundle_
方法显示。您可以在此处查看 Altair 的定义:https://github.com/altair-viz/altair/blob/v4.1.0/altair/vegalite/v4/api.py#L1644-L1654
您可以在 IPython 文档中阅读更多相关信息:Integrating your objects with IPython。
创建 RandomWalk
class 的最简单方法可能如下所示:
class RandomWalk:
def __init__(self, n):
self._data = pd.DataFrame({
'x': np.arange(n) + 1,
'y': np.cumsum(np.random.normal(size=n)),
})
self._chart = alt.Chart(self._data).mark_line().encode(x='x', y='y')
def _repr_mimebundle_(self, include=None, exclude=None):
return self._chart._repr_mimebundle_(include, exclude)
我有一个 class 想在 JupyterLab 中使用。当 JupyterLab 笔记本中的最后一条语句计算出我的 class 的一个实例时,我想显示一个 Altair Chart
代表我的 class 的对象。推荐的方法是什么?在下面的示例中,似乎在这种情况下我的 class 上调用了 repr
函数,所以这是一个钩子,我可以用它来控制当 JupyterLab 笔记本中的最后一条语句计算时发生的事情到我的 class 的一个实例。但是,我不确定下一步该怎么做。我可以在代表我的对象的 Altair Chart
上调用 display
,然后 return 一个空字符串。但是,return 是空字符串而不是更有意义的字符串似乎有点奇怪。例如,如果我想在其他上下文中使用 repr
来显示对象的表示,那么我将无法做到。当我在 Altair Chart
上调用 repr
时,我得到一个类似于 alt.Chart(...)
的字符串,因此似乎必须有其他方法来控制 JupyterLab 单元格中的最后一条语句时发生的情况是一些对象。有更好的方法吗?
import altair as alt
import numpy as np
import pandas as pd
class RandomWalk:
def __init__(self, n):
self._data = pd.DataFrame({
'x': np.arange(n) + 1,
'y': np.cumsum(np.random.normal(size=n)),
})
self._chart = alt.Chart(self._data).mark_line().encode(x='x', y='y')
def __repr__(self):
# When I use the following line for the body of __repr__, then
# JupyterLab prints the string 'alt.Chart(...)' and does not display
# the chart.
# return repr(self._chart)
# The following line displays the chart as desired, but it feels odd to
# then also return the empty string.
self._chart.display()
return ''
RandomWalk(100)
Altair 图表通过 Jupyter 的 _repr_mimebundle_
方法显示。您可以在此处查看 Altair 的定义:https://github.com/altair-viz/altair/blob/v4.1.0/altair/vegalite/v4/api.py#L1644-L1654
您可以在 IPython 文档中阅读更多相关信息:Integrating your objects with IPython。
创建 RandomWalk
class 的最简单方法可能如下所示:
class RandomWalk:
def __init__(self, n):
self._data = pd.DataFrame({
'x': np.arange(n) + 1,
'y': np.cumsum(np.random.normal(size=n)),
})
self._chart = alt.Chart(self._data).mark_line().encode(x='x', y='y')
def _repr_mimebundle_(self, include=None, exclude=None):
return self._chart._repr_mimebundle_(include, exclude)