Gui*_*ido 3 file ipython julia
在我的带有iJulia的iPython笔记本中,是否可以调用(函数)其他文件?到目前为止,我使用所有方法在一个大的.ipynb文件中工作,但它变得太大了.有没有办法将一些功能传输到其他文件,以便从那里调用它们?
您可以在.jl文件中定义函数,然后include在笔记本中定义函数.
如果您有一个test.jl使用内容调用的文件:
function helloworld()
println("Hello, World!")
end
Run Code Online (Sandbox Code Playgroud)
然后,您可以从笔记本中调用它,它将被评估.然后,您可以正常使用文件中定义的函数:
In [1]: include("test.jl")
Out[1]: helloworld (generic function with 1 method)
In [2]: helloworld()
Hello, world!
Run Code Online (Sandbox Code Playgroud)
编辑:从另一个ipynb文件运行代码是非常棘手的,因为代码被整理到笔记本的json中.如果你真的想要这个功能应该工作:
using PyCall
function execute_notebook(nbfile)
@pyimport IPython.nbformat.current as ipyt
open(nbfile) do f
nbstring = readall(f)
nb = ipyt.reads(nbstring, "json")
for cell in nb["worksheets"][1]["cells"]
eval(parse(cell["input"]))
end
end
end
Run Code Online (Sandbox Code Playgroud)
它目前抛出一个错误,但它似乎仍然有效.如果你有一个定义test.ipynb了相同的helloworld()功能,你可以从另一个笔记本中调用它:
execute_notebook("test.ipynb")
helloworld()
Run Code Online (Sandbox Code Playgroud)
我仍然建议保留您要从.jl文件中的其他位置而不是文件中调用的代码.ipynb.它更简单,可能更强大.
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |