user15929966
user15929966

Reputation:

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.

Upvotes: 2

Views: 726

Answers (1)

matan h
matan h

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

Related Questions