如何在Jupyter笔记本中嵌入OpenMDAO N2图

时间:2017-08-28 13:24:16

标签: javascript jupyter openmdao

我正在尝试使用以下代码将openMdao生成的N2 digaram嵌入到Jupyter笔记本中:

from openmdao.api import Problem

from openmdao.examples.beam_tutorial import BeamTutorial
from openmdao.api import view_model
from IPython.core.display import display, HTML
top = Problem()
top.root = BeamTutorial()

top.setup(check=False)
view_model(top, embed=True, show_browser=False, 
outfile='partition_tree_n2.html')
display(HTML(filename='partition_tree_n2.html'))

这会正确显示图表的工具栏但没有图表,并且还会显示错误:

Javascript error adding output!
SyntaxError: Unexpected token >
See your browser Javascript console for more details.

Javascript控制台上的错误是:

SyntaxError: Unexpected token >
at eval (<anonymous>)
at Function.globalEval (jquery.min.js:4)
at init.domManip (jquery.min.js:5)
at init.append (jquery.min.js:5)
at OutputArea._safe_append (outputarea.js:440)
at OutputArea.append_display_data (outputarea.js:638)
at OutputArea.append_output (outputarea.js:330)
at OutputArea.handle_output (outputarea.js:243)
at output (codecell.js:365)
at Kernel._handle_output_message (kernel.js:1196))

如何解决此问题或以其他方式将OpenMDAO N2图表嵌入Jupyter笔记本?

1 个答案:

答案 0 :(得分:0)

经过更多研究后我相信这是因为html包含HTML中不允许使用的javascript,一种可能的解决方案是使用IFrame,如下所示:

from openmdao.api import Problem

from openmdao.examples.beam_tutorial import BeamTutorial
from openmdao.api import view_model
from IPython.core.display import display, HTML
from IPython.display import IFrame
top = Problem()
top.root = BeamTutorial()

top.setup(check=False)
view_model(top, embed=True, show_browser=False, outfile='partition_tree_n2.html')
display(IFrame(src='partition_tree_n2.html',width=1300,height=700))

如果有人想提出更好的答案,以编程方式设置宽度和高度仍然会很好。