简体   繁体   English

ipython笔记本中的自定义魔法 - 代码无法执行

[英]Custom magic in ipython notebooks - Code does not execute

I am relatively new to ipython magics and want to run some code and at the same time add it to a list through the magic commands. 我对ipython魔法相对较新,想要运行一些代码,同时通过魔术命令将其添加到列表中。 The magics are defined as follows 魔法定义如下

#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)

and I am calling the magics as follows 而我正在调用魔法如下

%%add_conf_code_to_report

import pandas as pd
import numpy as np
import os
import collections

The import code is copied to the _conf_code_store alright but I cannot call the functions from the imported libraries. 导入代码被复制到_conf_code_store,但我无法从导入的库中调用这些函数。 I want that the code should be added to the _conf_code_store and at the same time the imported libaries' functionality should be available in the notebook. 我希望代码应该添加到_conf_code_store中,同时导入的libaries的功能应该在笔记本中可用。

I have been able to get a work around it. 我已经能够解决它。 To execute a code through the magic function one has to call run_cell instance for the ipython object. 要通过magic函数执行代码,必须为ipython对象调用run_cell实例。 There can be better ways of doing it but the code works for now. 可以有更好的方法,但代码现在可以使用。

@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM