在Jupyter Notebook

时间:2017-05-05 06:04:15

标签: svg ipython jupyter

我想在一个jupyter单元格中嵌入一个外部.svg,然后能够平移和缩放。具体来说,我想使用伟大的svg-pan-zoom库。考虑到类似的扩展,例如传单和mapbox,这似乎是可能的。

只是尝试svg-pan-zoom中的示例,给出以下内容:

%%HTML
<html>
<body>
<h1>Demo for svg-pan-zoom: SVG in HTML 'object' element</h1>
<object id="demo-tiger" type="image/svg+xml" data="https://cdn.rawgit.com/ariutta/svg-pan-zoom/master/demo/tiger.svg" style="width: 500px; height: 500px; border:1px solid black; ">Your browser does not support SVG</object>
<script>
require.config({
  paths: {
      panzoom: '//cdn.rawgit.com/ariutta/svg-pan-zoom/master/dist/svg-pan-zoom'
  }
});
require(['panzoom'], function(panzoom) {
    console.log("Loaded :)"); 

    var s = document.getElementById('demo-tiger')
    s.onload = function(panzoom){
        console.log('loaded')

        panzoom.svgPanZoom('#demo-tiger', {
          zoomEnabled: true,
          controlIconsEnabled: true
        });
        }

    });
</script>
</body>
</html>

单元格渲染图像,但从不渲染平移缩放功能。经过许多不同的方法后,它仍然无法将svgPanZoom识别为函数。

我也尝试将其加载到/nbextensions目录中。这也行不通。

我对JS或DOM没有经验,所以感谢任何建议。

2 个答案:

答案 0 :(得分:0)

window.onload 由于显而易见的原因在 Jupyter 中无法正常工作。 请参阅 https://github.com/vlasenkoalexey/tfgraphviz/blob/master/tfgraphviz/jupyter_helper.py 以了解有效的辅助方法。

答案 1 :(得分:-1)

此线程中有一个在笔记本中使用平移缩放的工作示例:

https://community.deepnote.com/c/forum/svg-pan-zoom?expand_comment_id=623583&login_token=eZ3YucZ66GXYMtfxagm6B53rdAX5JHAkYsoK#comment_623583

%%html
  <body>
<script src="https://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.js"></script>
<div id="container" style="width: 500px; height: 300px; border:1px solid black; ">
<svg id="demo-tiger" xmlns="http://www.w3.org/2000/svg" style="display: inline; width: inherit; min-width: inherit; max-width: inherit; height: inherit; min-height: inherit; max-height: inherit; " viewBox="0 0 900 900">
  <circle cx="50" cy="50" r="50" />
</svg>
</div>
 <button id="enable">enable</button>
    <button id="disable">disable</button>

    <script>
      // Don't use window.onLoad like this in production, because it can only listen to one function.
      window.onload = function() {
        // Expose to window namespase for testing purposes
        window.zoomTiger = svgPanZoom('#demo-tiger', {
          zoomEnabled: true,
          controlIconsEnabled: true,
          fit: true,
          center: true,
          // viewportSelector: document.getElementById('demo-tiger').querySelector('#g4') // this option will make library to misbehave. Viewport should have no transform attribute
        });

        document.getElementById('enable').addEventListener('click', function() {
          window.zoomTiger.enableControlIcons();
        })
        document.getElementById('disable').addEventListener('click', function() {
          window.zoomTiger.disableControlIcons();
        })
      };
    </script>
  </body>