在jupyter notebook中调用.ipynb文件方式


Posted in Python onApril 14, 2020

正常来说在jupyter notebook 中只能调用.py文件,要想要调用jupyter notebook自己的文件会报错。

Jupyter Notebook官网介绍了一种简单的方法:

http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html

添加jupyter notebook解析文件

首先,创建一个python文件,例如Ipynb_importer.py,代码如下:

import io, os,sys,types
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell

class NotebookFinder(object):
 """Module finder that locates Jupyter Notebooks"""
 def __init__(self):
  self.loaders = {}

 def find_module(self, fullname, path=None):
  nb_path = find_notebook(fullname, path)
  if not nb_path:
   return

  key = path
  if path:
   # lists aren't hashable
   key = os.path.sep.join(path)

  if key not in self.loaders:
   self.loaders[key] = NotebookLoader(path)
  return self.loaders[key]

def find_notebook(fullname, path=None):
 """find a notebook, given its fully qualified name and an optional path

 This turns "foo.bar" into "foo/bar.ipynb"
 and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
 does not exist.
 """
 name = fullname.rsplit('.', 1)[-1]
 if not path:
  path = ['']
 for d in path:
  nb_path = os.path.join(d, name + ".ipynb")
  if os.path.isfile(nb_path):
   return nb_path
  # let import Notebook_Name find "Notebook Name.ipynb"
  nb_path = nb_path.replace("_", " ")
  if os.path.isfile(nb_path):
   return nb_path

class NotebookLoader(object):
 """Module Loader for Jupyter Notebooks"""
 def __init__(self, path=None):
  self.shell = InteractiveShell.instance()
  self.path = path

 def load_module(self, fullname):
  """import a notebook as a module"""
  path = find_notebook(fullname, self.path)

  print ("importing Jupyter notebook from %s" % path)

  # load the notebook object
  with io.open(path, 'r', encoding='utf-8') as f:
   nb = read(f, 4)


  # create the module and add it to sys.modules
  # if name in sys.modules:
  # return sys.modules[name]
  mod = types.ModuleType(fullname)
  mod.__file__ = path
  mod.__loader__ = self
  mod.__dict__['get_ipython'] = get_ipython
  sys.modules[fullname] = mod

  # extra work to ensure that magics that would affect the user_ns
  # actually affect the notebook module's ns
  save_user_ns = self.shell.user_ns
  self.shell.user_ns = mod.__dict__

  try:
   for cell in nb.cells:
   if cell.cell_type == 'code':
    # transform the input to executable Python
    code = self.shell.input_transformer_manager.transform_cell(cell.source)
    # run the code in themodule
    exec(code, mod.__dict__)
  finally:
   self.shell.user_ns = save_user_ns
  return mod
sys.meta_path.append(NotebookFinder())

调用jupyter notebook module

只要在我们的工作目录下放置Ipynb_importer.py文件,就可以正常调用所有的jupyter notebook文件。 这种方法的本质就是使用一个jupyter notenook解析器先对.ipynb文件进行解析,把文件内的各个模块加载到内存里供其他python文件调用。

新建一个文件foo.ipynb

def foo():
 print("foo")

再新建一个ipynb文件,调用foo这个文件

import Ipynb_importer
import foo
foo.foo()

运行结果如下:

importing Jupyter notebook from foo.ipynb
foo

补充知识:jupyter notebook_主函数文件如何调用类文件

使用jupyter notebook编写python程序,rw_visual.jpynb是写的主函数,random_walk.jpynb是类(如图)。在主函数中将类实例化后运行会报错,经网络查找解决了问题,缺少Ipynb_importer.py这样一个链接文件。

在jupyter notebook中调用.ipynb文件方式

解决方法:

1、在同一路径下创建名为Ipynb_importer.py的文件:File-->download as-->Python(.py),该文件内容如下:

#!/usr/bin/env python
# coding: utf-8
# In[ ]:
 
import io, os,sys,types
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell
 
class NotebookFinder(object):
 """Module finder that locates Jupyter Notebooks"""
 def __init__(self):
  self.loaders = {}
 
 def find_module(self, fullname, path=None):
  nb_path = find_notebook(fullname, path)
  if not nb_path:
   return
 
  key = path
  if path:
   # lists aren't hashable
   key = os.path.sep.join(path)
 
  if key not in self.loaders:
   self.loaders[key] = NotebookLoader(path)
  return self.loaders[key]
 
def find_notebook(fullname, path=None):
 """find a notebook, given its fully qualified name and an optional path
 This turns "foo.bar" into "foo/bar.ipynb"
 and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
 does not exist.
 """
 name = fullname.rsplit('.', 1)[-1]
 if not path:
  path = ['']
 for d in path:
  nb_path = os.path.join(d, name + ".ipynb")
  if os.path.isfile(nb_path):
   return nb_path
  # let import Notebook_Name find "Notebook Name.ipynb"
  nb_path = nb_path.replace("_", " ")
  if os.path.isfile(nb_path):
   return nb_path
 
class NotebookLoader(object):
 """Module Loader for Jupyter Notebooks"""
 def __init__(self, path=None):
  self.shell = InteractiveShell.instance()
  self.path = path
 
 def load_module(self, fullname):
  """import a notebook as a module"""
  path = find_notebook(fullname, self.path)
 
  print ("importing Jupyter notebook from %s" % path)
 
  # load the notebook object
  with io.open(path, 'r', encoding='utf-8') as f:
   nb = read(f, 4)
 
 
  # create the module and add it to sys.modules
  # if name in sys.modules:
  # return sys.modules[name]
  mod = types.ModuleType(fullname)
  mod.__file__ = path
  mod.__loader__ = self
  mod.__dict__['get_ipython'] = get_ipython
  sys.modules[fullname] = mod
 
  # extra work to ensure that magics that would affect the user_ns
  # actually affect the notebook module's ns
  save_user_ns = self.shell.user_ns
  self.shell.user_ns = mod.__dict__
 
  try:
   for cell in nb.cells:
   if cell.cell_type == 'code':
    # transform the input to executable Python
    code = self.shell.input_transformer_manager.transform_cell(cell.source)
    # run the code in themodule
    exec(code, mod.__dict__)
  finally:
   self.shell.user_ns = save_user_ns
  return mod
sys.meta_path.append(NotebookFinder())

2、在主函数中import Ipynb_importer

import matplotlib.pyplot as plt
import Ipynb_importer
 
from random_walk import RandomWalk
 
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s=15)
plt.show()

3、运行主函数,调用成功

ps:random_walk.jpynb文件内容如下:

from random import choice
 
class RandomWalk():
 def __init__(self, num_points=5000):
  self.num_points = num_points
  self.x_values = [0]
  self.y_values = [0]
  
 def fill_walk(self):
  while len(self.x_values) < self.num_points:
   x_direction = choice([1,-1])
   x_distance = choice([0,1,2,3,4])
   x_step = x_direction * x_distance
   
   y_direction = choice([1,-1])
   y_distance = choice([0,1,2,3,4])
   y_step = y_direction * y_distance
   
   if x_step == 0 and y_step == 0:
    continue
    
   next_x = self.x_values[-1] + x_step
   next_y = self.y_values[-1] + y_step
   
   self.x_values.append(next_x)
   self.y_values.append(next_y)

运行结果:

在jupyter notebook中调用.ipynb文件方式

以上这篇在jupyter notebook中调用.ipynb文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python列表操作使用示例分享
Feb 21 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
Apr 23 Python
python3 读写文件换行符的方法
Apr 09 Python
浅谈python在提示符下使用open打开文件失败的原因及解决方法
Nov 30 Python
python调用java的jar包方法
Dec 15 Python
Python根据成绩分析系统浅析
Feb 11 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
对python周期性定时器的示例详解
Feb 19 Python
Python3.9又更新了:dict内置新功能
Feb 28 Python
基于Pyinstaller打包Python程序并压缩文件大小
May 28 Python
Python基于百度AI实现抓取表情包
Jun 27 Python
python scrapy简单模拟登录的代码分析
Jul 21 Python
使用jupyter notebook将文件保存为Markdown,HTML等文件格式
Apr 14 #Python
Python使用pyyaml模块处理yaml数据
Apr 14 #Python
Jupyter Notebook打开任意文件夹操作
Apr 14 #Python
Python requests模块cookie实例解析
Apr 14 #Python
Python requests模块session代码实例
Apr 14 #Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
Apr 14 #Python
服务器端jupyter notebook映射到本地浏览器的操作
Apr 14 #Python
You might like
探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
2013/06/20 PHP
php 邮件发送问题解决
2014/03/22 PHP
让CodeIgniter的ellipsize()支持中文截断的方法
2014/06/12 PHP
PHP实现简单爬虫的方法
2015/07/29 PHP
CSS常用网站布局实例
2008/04/03 Javascript
JavaScript 原型链学习总结
2010/10/29 Javascript
JavaScript高级程序设计 阅读笔记(二十一) JavaScript中的XML
2012/09/14 Javascript
JavaScript常用全局属性与方法记录积累
2013/07/03 Javascript
jQuery中html()方法用法实例
2014/12/25 Javascript
详解AngularJS实现表单验证
2015/12/10 Javascript
详解JavaScript中双等号引起的隐性类型转换
2016/05/30 Javascript
js登录滑动验证的实现(不滑动无法登陆)
2018/01/03 Javascript
vue的安装及element组件的安装方法
2018/03/09 Javascript
手写简单的jQuery雪花飘落效果实例
2018/04/22 jQuery
js嵌套的数组扁平化:将多维数组变成一维数组以及push()与concat()区别的讲解
2019/01/19 Javascript
node静态服务器实现静态读取文件或文件夹
2019/12/03 Javascript
Python定时器实例代码
2017/11/01 Python
Python 实现选择排序的算法步骤
2018/04/22 Python
Python将文本去空格并保存到txt文件中的实例
2018/07/24 Python
把JSON数据格式转换为Python的类对象方法详解(两种方法)
2019/06/04 Python
详解Pycharm出现out of memory的终极解决方法
2020/03/03 Python
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
2020/03/16 Python
如何学习Python time模块
2020/06/03 Python
Python使用shutil模块实现文件拷贝
2020/07/31 Python
Pycharm github配置实现过程图解
2020/10/13 Python
python工具快速为音视频自动生成字幕(使用说明)
2021/01/27 Python
迪卡侬荷兰官网:Decathlon荷兰
2017/10/29 全球购物
NULL是什么,它是怎么定义的
2015/05/09 面试题
护士自我鉴定
2013/10/23 职场文书
垃圾分类的活动方案
2014/08/15 职场文书
教师自查自纠材料
2014/10/14 职场文书
同学聚会通知短信
2015/04/20 职场文书
2015年乡镇卫生院妇幼保健工作总结
2015/05/19 职场文书
海洋天堂观后感
2015/06/05 职场文书
简述python四种分词工具,盘点哪个更好用?
2021/04/13 Python
使用@Value值注入及配置文件组件扫描
2021/07/09 Java/Android