question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to define a custom (cell) magic

See original GitHub issue

This is more of an inquiry/research question: I recently started playing around with Jep a bit and found it quite useful for combining the Java image processing library ImgLib2 and Kotlin extensions/operators with NumPy ndarray in native CPython. This all works with shared memory, really cool! I learned about library descriptors, which I could use to add Jep and a Python interpreter to the Kernel. Users could then call

interpreter.exec

and similar functions to execute their Python code.

Then I thought, maybe I can mark an entire cell as Python code via some custom cell magic, and the magic handler would then pass the Python code to the Python interpreter object that the Kernel holds. To make this work, I would need some way (that may or may not exist) to add a custom cell magic. Before I spend more time on it, I would like to know if something like that would be possible, in principle, or if I would be wasting my time.

Thank you!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ileasilecommented, May 6, 2021

Hello @hanslovsky! Currently the list of magics is fixed and not extensible, but your idea is really cool. I think that I can implement even a more powerful way of doing it — custom code preprocessor which takes the whole cell code before the execution and can change it in any way. In your case it will wrap the cell code to the interpreter.exec() call.

0reactions
hanslovskycommented, May 15, 2021

I was also able to create a library definition the proper way (example notebooks in the repo). I am really happy with how this turned out. Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining custom magics — IPython 8.7.0 documentation
There are two main ways to define your own magic functions: from standalone functions and by inheriting from a base class provided by...
Read more >
Defining custom magics — IPython 3.2.1 documentation
There are two main ways to define your own magic functions: from standalone functions and by inheriting from a base class provided by...
Read more >
How do I define custom magics in jupyter? - Stack Overflow
There are two separate things here: startup files are scripts in ~/.ipython/profile_[name]/startup that are executed as part of starting ...
Read more >
Creating an IPython extension with custom magic commands
Although IPython comes with a wide variety of magic commands, there are cases where we need to implement custom functionality in new magic...
Read more >
1.4. Creating an IPython extension with custom magic ...
Defining a new line magic is particularly simple. First, we create a function that accepts the contents of the line (except the initial...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found