Issue
Is there an easy way to make ipython console redirect the cmd-line towards external parser and then output the result in the current session.
Say for example I have parser that calculates expressions (just for the sake of the example). Then I want when the cmd-line starts with "calc:" to pass it to this external parser ... here is hypothetical example :
In[XX]: calc: 5 + 5
external calc: 5 + 5 = 10
and so on, you get the idea..
this is the closest I found so far : first create a shell script :
#!/bin/sh
echo $1
then in ipython :
In [473]: !./x 123
123
if it is in system path then even shorter :
In [475]: !x 123
123
Now if I can share state across invocations.
Solution
I made it work as extension :
from __future__ import print_function
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, line_cell_magic)
from bi_lang import *
@magics_class
class BiMagics(Magics):
def __init__(self, shell):
super(BiMagics, self).__init__(shell)
self.bi = BiLang()
@line_magic
def do(self, line):
rv = self.bi.run(line)
return rv
ip = get_ipython()
magics = BiMagics(ip)
ip.register_magics(magics)
Then :
In [3]: %reload_ext ipython_extension
In [4]: %do 5 + 6
===== return ast =====
Value
+- val 11
`- vtype 'num'
If you need more info, look here : http://ipython.readthedocs.io/en/stable/config/custommagics.html#defining-magics
Answered By - sten
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.