在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程序实例(通讯录)
Nov 29 Python
在Python中使用第三方模块的教程
Apr 27 Python
Python 爬虫爬取指定博客的所有文章
Feb 17 Python
详解python里使用正则表达式的分组命名方式
Oct 24 Python
python机器学习之神经网络(三)
Dec 20 Python
Python数据分析之双色球基于线性回归算法预测下期中奖结果示例
Feb 08 Python
对pyqt5多线程正确的开启姿势详解
Jun 14 Python
Django 接收Post请求数据,并保存到数据库的实现方法
Jul 12 Python
Python算法的时间复杂度和空间复杂度(实例解析)
Nov 19 Python
简单了解python字符串前面加r,u的含义
Dec 26 Python
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
Mar 05 Python
python实现贪吃蛇游戏源码
Mar 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
zen cart新进商品的随机排序修改方法
2010/09/10 PHP
提高define性能的php扩展hidef的安装和使用
2011/06/14 PHP
PHP模块 Memcached功能多于Memcache
2011/06/14 PHP
php中配置文件操作 如config.php文件的读取修改等操作
2012/07/07 PHP
php mb_substr()函数截取中文字符串应用示例
2014/07/29 PHP
HTTP头隐藏PHP版本号实现过程解析
2020/12/09 PHP
防止动态加载JavaScript引起的内存泄漏问题
2009/10/08 Javascript
qTip 基于JQuery的Tooltip插件[兼容性好]
2010/09/01 Javascript
c#和Javascript操作同一json对象的实现代码
2012/01/17 Javascript
鼠标滚轮控制网页横向移动实现思路
2013/03/22 Javascript
jQuery实现点击标题输入详细信息
2013/04/16 Javascript
详解AngularJS中自定义指令的使用
2015/06/17 Javascript
javascript日期格式化方法小结
2015/12/17 Javascript
分享几种比较简单实用的JavaScript tabel切换
2015/12/31 Javascript
jQuery轻松实现表格的隔行变色和点击行变色的实例代码
2016/05/09 Javascript
原生js实现自由拖拽弹窗代码demo
2016/06/29 Javascript
高性能的javascript之加载顺序与执行原理篇
2018/01/14 Javascript
详解Jest结合Vue-test-utils使用的初步实践
2019/06/27 Javascript
layer.prompt输入层的例子
2019/09/24 Javascript
Vue 中 a标签上href无法跳转的解决方式
2019/11/12 Javascript
vue中destroyed方法的使用说明
2020/07/21 Javascript
原生JS实现拖拽功能
2020/12/16 Javascript
python开发之str.format()用法实例分析
2016/02/22 Python
Python3 加密(hashlib和hmac)模块的实现
2017/11/23 Python
django+echart绘制曲线图的方法示例
2018/11/26 Python
python 3.6.7实现端口扫描器
2019/09/04 Python
Python调用Windows API函数编写录音机和音乐播放器功能
2020/01/05 Python
python使用Geany编辑器配置方法
2020/02/21 Python
python实现打砖块游戏
2020/02/25 Python
Python图像处理库PIL的ImageFilter模块使用介绍
2020/02/26 Python
海淘母婴商城:国际妈咪
2016/07/23 全球购物
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
邻里守望志愿服务活动方案
2014/08/15 职场文书
2015年汽车销售员工作总结
2015/07/24 职场文书
城镇居民医疗保险工作总结
2015/08/10 职场文书
python文件名批量重命名脚本实例代码
2021/04/22 Python