ipython笔记本中的自定义魔术-代码不执行
我对ipython magics比较陌生,希望运行一些代码,同时通过magic命令将其添加到列表中。魔法的定义如下ipython笔记本中的自定义魔术-代码不执行,python,ipython,Python,Ipython,我对ipython magics比较陌生,希望运行一些代码,同时通过magic命令将其添加到列表中。魔法的定义如下 #iPython notebook magic from IPython.core.magic import ( Magics, magics_class, cell_magic, line_magic ) @magics_class class ReportMagic(Magics): def __init__(self, shell, data):
#iPython notebook magic
from IPython.core.magic import (
Magics, magics_class, cell_magic, line_magic
)
@magics_class
class ReportMagic(Magics):
def __init__(self, shell, data):
super(ReportMagic,self).__init__(shell)
self._code_store = []
self._markdown_store = []
self._conf_code_store=[]
self._conf_markdown_store=[]
self.data = data
# inject our store in user availlable namespace under __mystore
# name
shell.user_ns['__mycodestore'] = self._code_store
shell.user_ns['__mymarkdownstore'] = self._markdown_store
@cell_magic
def add_code_to_report(self, line, cell):
"""store the cell in the store"""
self._code_store.append(cell)
@cell_magic
def add_markdown_to_report(self, line, cell):
"""store the cell in the store"""
self._markdown_store.append(cell)
@cell_magic
def add_conf_code_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_code_store.append(cell)
@cell_magic
def add_conf_markdown_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_markdown_store.append(cell)
@line_magic
def show_report(self, line):
"""show all recorded statements"""
return self._conf_markdown_store,self._conf_code_store ,self._markdown_store,self._code_store
# This class must then be registered with a manually created instance,
# since its constructor has different arguments from the default:
ip = get_ipython()
magics = ReportMagic(ip, 0)
ip.register_magics(magics)
%%add_conf_code_to_report
import pandas as pd
import numpy as np
import os
import collections
我给魔术师打电话如下
#iPython notebook magic
from IPython.core.magic import (
Magics, magics_class, cell_magic, line_magic
)
@magics_class
class ReportMagic(Magics):
def __init__(self, shell, data):
super(ReportMagic,self).__init__(shell)
self._code_store = []
self._markdown_store = []
self._conf_code_store=[]
self._conf_markdown_store=[]
self.data = data
# inject our store in user availlable namespace under __mystore
# name
shell.user_ns['__mycodestore'] = self._code_store
shell.user_ns['__mymarkdownstore'] = self._markdown_store
@cell_magic
def add_code_to_report(self, line, cell):
"""store the cell in the store"""
self._code_store.append(cell)
@cell_magic
def add_markdown_to_report(self, line, cell):
"""store the cell in the store"""
self._markdown_store.append(cell)
@cell_magic
def add_conf_code_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_code_store.append(cell)
@cell_magic
def add_conf_markdown_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_markdown_store.append(cell)
@line_magic
def show_report(self, line):
"""show all recorded statements"""
return self._conf_markdown_store,self._conf_code_store ,self._markdown_store,self._code_store
# This class must then be registered with a manually created instance,
# since its constructor has different arguments from the default:
ip = get_ipython()
magics = ReportMagic(ip, 0)
ip.register_magics(magics)
%%add_conf_code_to_report
import pandas as pd
import numpy as np
import os
import collections
导入代码被复制到_conf_code_存储区,但我无法从导入的库调用函数。
我希望代码应该被添加到_conf_code_store中,同时导入的库功能应该可以在笔记本中使用。我已经能够找到解决方法。 要通过magic函数执行代码,必须为ipython对象调用run_单元实例。有更好的方法可以做到这一点,但代码目前仍然有效
@cell_magic
@needs_local_scope
def add_conf_code_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_code_store.append(cell)
print type(cell)
exec 'from IPython.core.display import HTML'
for each in cell.split('\n'):
print each
exec repr(each.strip())
ip=get_ipython()
ip.run_cell(cell)
我已经找到了一个解决办法。 要通过magic函数执行代码,必须为ipython对象调用run_单元实例。有更好的方法可以做到这一点,但代码目前仍然有效
@cell_magic
@needs_local_scope
def add_conf_code_to_report(self, line, cell):
"""store the cell in the store"""
self._conf_code_store.append(cell)
print type(cell)
exec 'from IPython.core.display import HTML'
for each in cell.split('\n'):
print each
exec repr(each.strip())
ip=get_ipython()
ip.run_cell(cell)