Display vega spec in Jupyter Lab

别说谁变了你拦得住时间么 提交于 2021-02-11 12:50:28

问题


How can I display a vega spec (such as this force directed layout) in Jupyter Lab/JupyterLab?


回答1:


You can use Vega Embed through the javascript extension:

Add the scripts:

%%javascript
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '//cdn.jsdelivr.net/npm/vega@5';
    document.head.appendChild(script);
    
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '//cdn.jsdelivr.net/npm/vega-embed@6';
    document.head.appendChild(script);

then

from IPython.display import Javascript
script = '''
var spec = "https://raw.githubusercontent.com/vega/vega/master/docs/examples/bar-chart.vg.json";
vegaEmbed(element, spec).then(function(result) {
  }).catch(console.error);  
'''

Javascript(script)

Note: The example force directed spec at https://raw.githubusercontent.com/vega/vega/master/docs/examples/force-directed-layout.vg.json doesn't display because it references data at a relative url (data/miserables.json). The bar chart works because the data is encoded directly into the spec.



来源:https://stackoverflow.com/questions/66060197/display-vega-spec-in-jupyter-lab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!