1

I have a special Class that capture and process the logs of a given algorithm. This looks like this

report = Report2File(logger,"./path")
report.start()

solveProblem()

report.stop()
del report

I would like to be event more lazy and write only

%%report "./path"
solveProblem()

The creation of such Magic Cell seams easy at first

UPDATED:

@magics_class
class MyMagics(Magics):

    @cell_magic
    def cmagic(self, line, cell):
        "my cell magic"
        self.before()
        exec(cell)
        self.after()
        return line, cell

    def before(self):
        do stuff ...

    def after(self):
        do stuff ...

ip = get_ipython()
ip.register_magics(MyMagics)

However I get two issues:

  1. I don't know how to pass the logger object to my magic
  2. Jupyter keep telling me that the MyMagics module is not an ipython extension

Partial Answer

this talk gives me the answer to 2.

Instead of

ip = get_ipython()
ip.register_magics(MyMagics)

The correct way to register a Magic is the following

def load_ipython_extension(ip):
    ip.register_magics(MyMagics)

def unload_ipython_extension(ip):
    pass
xNok
  • 98
  • 1
  • 7

1 Answers1

0

You also need to decorate the class with @magics_class (from IPython.core.magic).
To pass arguments, look at the @magic_arguments documentation for examples: it's pretty easy to follow.

cco
  • 5,873
  • 1
  • 16
  • 21
  • `@magics_class` is necessary true but it is not enough to solve both issue. I just forgot to mentioned it. [this talk give me the answer](https://www.youtube.com/watch?v=zxkdO07L29Q) – xNok Mar 10 '17 at 16:31