Reputation:
I have been using some jupyter notebook magics such as %matplotlib inline.
How to load them using python script instead of % signs.
%load_ext sql
%config SqlMagic.displaycon=False
# file: myimports.py
from something import x
x.load_ext('sql')
x.config('SqlMagic.displaycon',False)
I want to use import * from myimports.py
in jupyter notebook, so that I don't have to type %load_ext sql
and so many things everytime I create new jupyter notebooks.
Upvotes: 2
Views: 726
Reputation: 1148
You can run IPython line magic using IPython interactiveshell
:
from IPython.terminal.interactiveshell import TerminalInteractiveShell
shell:TerminalInteractiveShell = TerminalInteractiveShell.instance()
shell.run_line_magic("load_ext","sql")
shell.run_line_magic("config","SqlMagic.displaycon=False")
# or shell.config["SqlMagic"]={'displaycon': False}
run_line_magic(magic_name, line_after_magic)
you can even run cd
shell.run_line_magic("cd","..")
And it will work.
However, there is some magic that not working with TerminalInteractiveShell
because it can't access your code (alias, page, ...).
Upvotes: 4