# few functions that make it possible to import functions
# from jupyter notebooks as from modules;
# source: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html import io, os, sys, types from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell 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 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] sys.meta_path.append(NotebookFinder())

module_loader.py的更多相关文章

  1. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  2. Python导入其他文件中的.py文件 即模块

    import sys sys.path.append("路径") import .py文件

  3. import renumber.py in pymol

    cp renumber.py /usr/local/lib/python2.7/dist-packages/pymol import renumber or run /path/to/renumber ...

  4. python gettitle.py

    #!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...

  5. 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!

    情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...

  6. caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例

    caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...

  7. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  8. MySqlNDB使用自带的ndb_setup.py安装集群

    在用Mysql做集群时,使用Mysql的NDB版本更易于集群的扩展,稳定和数据的实时性. 我们可以使用Mysql自带的工具进行集群安装与管理:ndb_setup.py.位于Mysql的安装目录bin下 ...

  9. 将做好的py文件打包成模块,供别人安装调用

    现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...

随机推荐

  1. PHP的内存回收(GC)

    php官方对gc的介绍:http://php.net/manual/zh/features.gc.php

  2. JS 验证输入框输入 只允许输入正实数(正整数,正小数),其他情况下不能输入 oninput事件

    input标签的oninput事件 要求输入框只输入正实数,包括整数和小数. 具体要求:整数部分不超过7位,可以没有小数,若有位数不超过2位. <input type="text&qu ...

  3. laravel自定义门面

    https://learnku.com/articles/19195   关于laravel门面和服务提供者使用的一点见解,门面之词,不足之处,还请多多指教. 在laravel中,我们可能需要用到自己 ...

  4. [转帖]SAP S/4 HANA与SAP Business Suite/R3(ECC)的区别

    SAP S/4 HANA与SAP Business Suite/R3(ECC)的区别 https://blog.csdn.net/zhongguomao/article/details/5351520 ...

  5. ELK日志系统+x-pack安全验证

    根据之前已经搭好的ELK系统,现在加一个x-pack插件上去,不然谁拿到ip和端口都可以访问elasticsearch和kibana. 要的效果如下:打开kibana界面的时候要让其输入用户名密码才能 ...

  6. 校园电商项目3(基于SSM)——配置Maven

    步骤一:添加必要文件夹 先在src/main/resources下添加两个文件夹 接着在webapp文件夹下添加一个resources文件夹存放我们的静态网页内容 WEB-INF里的文件是不会被客户端 ...

  7. Linux基础操作二

    编程语言的作用及与操作系统和硬件的关系 编程语言的作用:用来定义计算机程序的形式,程序员用它来编写程序,进而控制其向计算机发出指令,使计算机完成人类布置的任务. 编程语言的作用及与操作系统和硬件的关系 ...

  8. 小程序获取当前页面URL

    var pages = getCurrentPages() //获取加载的页面 var currentPage = pages[pages.length-1] //获取当前页面的对象 var url ...

  9. python 字符串常用操作方法

    python 字符串常用操作方法 python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等 1.去除空格 str.strip():删除字符串两边的指定字符,括号的写 ...

  10. excel 公式 insert 语句

    ="insert into tb_fdn_deviceaccount (zdmc,czmc,sbbh,sbmc,SZCS,SBFLMC,SBLXMC,SBGG,SBYZ,SBJZ,SBXH, ...