'How to replace jupyter-notebook magic with python script
I have been using some jupyter notebook magics such as %matplotlib inline.
How to load them using python script instead of % signs.
Using jupyter
%load_ext sql
%config SqlMagic.displaycon=False
Question: How to do the same using script?
# 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.
Solution 1:[1]
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, ...).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
