简体   繁体   English

Altair:指定默认启用的渲染器

[英]Altair: specify a renderer that's enabled by default

Is there a way to enable a renderer except for calling alt.renderers.enable('mimebundle') in code?除了在代码中调用alt.renderers.enable('mimebundle')之外,有没有办法启用渲染器? So if the user imports altair she doesn't have to perform any additional actions?那么,如果用户导入 altair,她就不必执行任何其他操作?

For example, in plotly you can set an environment variable PLOTLY_RENDERER=plotly_mimetype .例如,在 plotly 中,您可以设置环境变量PLOTLY_RENDERER=plotly_mimetype Is there something similar in altair? Altair 有类似的东西吗?

No, Altair does not currently have any mechanism to specify a renderer aside from calling alt.renderers.enable .不,除了调用alt.renderers.enable之外,Altair 目前没有任何机制来指定渲染器。

But if you are using Jupyter, you could provide an IPython startup script that does this;但是如果你使用 Jupyter,你可以提供一个 IPython 启动脚本来执行此操作; for example, you can create a file at the path ~/.ipython/profile_default/startup/start.py with the following contents:例如,您可以在~/.ipython/profile_default/startup/start.py路径下创建一个包含以下内容的文件:

import altair
altair.renderers.enable('notebook')

and this will be executed at the start of any Jupyter/IPython session.这将在任何 Jupyter/IPython 会话开始时执行。

If you don't wish to import Altair in every session, you could instead define in this file a Python import hook that will execute custom code the first time Altair is imported.如果您不想在每个会话中都导入 Altair,您可以改为在此文件中定义一个Python 导入挂钩,该挂钩将在第一次导入 Altair 时执行自定义代码。 For example, it might look something like this:例如,它可能看起来像这样:

import imp
import os
import sys

class _AltairImportHook(object):
  def find_module(self, fullname, path=None):
    if fullname != 'altair':
      return None
    self.module_info = imp.find_module(fullname, path)
    return self

  def load_module(self, fullname):
    """Loads Altair normally and runs pre-initialization code."""
    previously_loaded = fullname in sys.modules
    altair = imp.load_module(fullname, *self.module_info)

    if not previously_loaded:
      try:
        altair.renderers.enable('notebook')
      except:
        pass
    return altair

sys.meta_path = [_AltairImportHook()] + sys.meta_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM