在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的迭代器、生成器以及相关的itertools包
Apr 02 Python
Python语言实现获取主机名根据端口杀死进程
Mar 31 Python
django创建自定义模板处理器的实例详解
Aug 14 Python
python中使用print输出中文的方法
Jul 16 Python
opencv python 基于KNN的手写体识别的实例
Aug 03 Python
解决pandas.DataFrame.fillna 填充Nan失败的问题
Nov 06 Python
PyQt5 QListWidget选择多项并返回的实例
Jun 17 Python
Python 脚本的三种执行方式小结
Dec 21 Python
Python内存映射文件读写方式
Apr 24 Python
python支持多继承吗
Jun 19 Python
使用Python封装excel操作指南
Jan 29 Python
python 通过使用Yolact训练数据集
Apr 06 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
preg_match_all使用心得分享
2014/01/31 PHP
php中cookie实现二级域名可访问操作的方法
2014/11/11 PHP
PHP和Mysql中转UTF8编码问题汇总
2015/10/10 PHP
PHP YII框架开发小技巧之模型(models)中rules自定义验证规则
2015/11/16 PHP
Laravel5.1框架路由分组用法实例分析
2020/01/04 PHP
jQuery学习笔记之jQuery的动画
2010/12/22 Javascript
使用EVAL处理jqchart jquery 折线图返回数据无效的解决办法
2015/11/26 Javascript
NodeJS中的MongoDB快速入门详细教程
2016/11/11 NodeJs
angularjs中ng-attr的用法详解
2016/12/31 Javascript
Node.js 使用流实现读写同步边读边写功能
2017/09/11 Javascript
在vue项目创建的后初始化首次使用stylus安装方法分享
2018/01/25 Javascript
在移动端使用vue-router和keep-alive的方法示例
2018/12/02 Javascript
中级前端工程师必须要掌握的27个JavaScript 技巧(干货总结)
2019/09/23 Javascript
js计时事件实现圆形时钟
2020/03/25 Javascript
TensorFlow.js 微信小程序插件开始支持模型缓存的方法
2020/02/21 Javascript
VUE Elemen-ui之穿梭框使用方法详解
2021/01/19 Javascript
浅析python 中__name__ = '__main__' 的作用
2014/07/05 Python
python中执行shell的两种方法总结
2017/01/10 Python
Python实现简单的多任务mysql转xml的方法
2017/02/08 Python
详解K-means算法在Python中的实现
2017/12/05 Python
Python实现的本地文件搜索功能示例【测试可用】
2018/05/30 Python
使用Python读取二进制文件的实例讲解
2018/07/09 Python
Python3常用内置方法代码实例
2019/11/18 Python
Pycharm快捷键配置详细整理
2020/10/13 Python
一款纯css3实现的鼠标经过按钮特效教程
2014/11/09 HTML / CSS
HTML5 Geolocation API的正确使用方法
2018/12/04 HTML / CSS
HTML5 Canvas——用路径描画线条实例介绍
2013/06/09 HTML / CSS
使用phonegap操作数据库的实现方法
2017/03/31 HTML / CSS
AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码
2020/08/19 HTML / CSS
香港卓悦化妆品官网:BONJOUR
2017/09/21 全球购物
满月酒答谢词
2014/01/14 职场文书
护士求职自荐信范文
2015/03/04 职场文书
五一晚会主持词
2015/07/01 职场文书
讲座新闻稿
2015/07/18 职场文书
公司人事管理制度
2015/08/05 职场文书
详解MySQL多版本并发控制机制(MVCC)源码
2021/06/23 MySQL