Show More
@@ -0,0 +1,45 b'' | |||
|
1 | import IPython.ipapi | |
|
2 | ip = IPython.ipapi.get() | |
|
3 | ||
|
4 | from IPython.external.simplegeneric import generic | |
|
5 | ||
|
6 | @generic | |
|
7 | def format_for_leo(obj): | |
|
8 | """ Convert obj to string representiation (for editing in Leo)""" | |
|
9 | return str(obj) | |
|
10 | ||
|
11 | @format_for_leo.when_type(list) | |
|
12 | def format_list(obj): | |
|
13 | return "\n".join(str(s) for s in obj) | |
|
14 | ||
|
15 | ||
|
16 | def add_var(self,varname): | |
|
17 | ip = self.getapi() | |
|
18 | leo = ip.user_ns['leox'] | |
|
19 | c,g = leo.c, leo.g | |
|
20 | nodename = '@ipy-var ' + varname | |
|
21 | p2 = g.findNodeAnywhere(c,nodename) | |
|
22 | if not c.positionExists(p2): | |
|
23 | p2 = c.currentPosition().insertAfter() | |
|
24 | c.setHeadString(p2,'@ipy-var ' + varname) | |
|
25 | ||
|
26 | c.setCurrentPosition(p2) | |
|
27 | val = ip.user_ns[varname] | |
|
28 | formatted = format_for_leo(val) | |
|
29 | c.setBodyString(p2,formatted) | |
|
30 | ||
|
31 | def leo_f(self,s): | |
|
32 | ip = self.getapi() | |
|
33 | s = s.strip() | |
|
34 | if s in ip.user_ns: | |
|
35 | add_var(self,s) | |
|
36 | ||
|
37 | ip.expose_magic('leo',leo_f) | |
|
38 | ||
|
39 | ||
|
40 | ||
|
41 | ||
|
42 | ||
|
43 | ||
|
44 | ||
|
45 |
General Comments 0
You need to be logged in to leave comments.
Login now