Unsplash
Data scientists and data analysts frequently utilise Jupyter Notebooks as a tool to begin their analyses. And there are a few important Jupyter notebook tricks or commands that are hardly ever used by the community.
In this post, we’ll execute commands which will make us a better user of Jupyter notebooks. Only pre-requisite to use magic commands is to install Ipython-kernel
pip install ipykernel
%%time
count = 0
for i in range(10000):
count += i
CPU times: user 1.39 ms, sys: 0 ns, total: 1.39 ms
Wall time: 1.4 ms
%time print("a")
%time print("b")
a
CPU times: user 35 µs, sys: 0 ns, total: 35 µs
Wall time: 37.9 µs
b
CPU times: user 8 µs, sys: 0 ns, total: 8 µs
Wall time: 9.06 µs
%%writefile addition.py
def add(a, b):
return a+b
add(5, 6)
In the above code, the function add or the lines below “%%writefile addition.py” is pushed into addition.py file. It will overwrite existing file or create a new file.
%load addition.py
On running above code, the contents of the file will be loaded into jupyter cell as follows
# %load addition.py
def add(a, b):
return a+b
add(5, 6)
%run addition.py
11
%env
{'ELECTRON_RUN_AS_NODE': '1', 'USER': 'mayur', 'LANGUAGE': 'en_IN:en', 'TEXTDOMAIN': 'im-config', 'XDG_SEAT': 'seat0', 'XDG_SESSION_TYPE': 'x11', 'SSH_AGENT_PID': '1711', 'SHLVL': '1',
...
'PYDEVD_IPYTHON_COMPATIBLE_DEBUGGING': '1'}
%set_env NEW_PATH=/usr/bin/python1
You can verify the newly created environment variable using %env.
%who
count i
%who int
count i
%lsmagic
{"line":{"automagic":"AutoMagics","autocall":"AutoMagics","alias_magic":"BasicMagics","lsmagic":"BasicMagics","magic":"BasicMagics","page":"BasicMagics","pprint":"BasicMagics","colors":"BasicMagics","xmode":"BasicMagics","quickref":"BasicMagics","doctest_mode":"BasicMagics","gui":"BasicMagics",..............
"python3":"Other","pypy":"Other","SVG":"Other","HTML":"Other","file":"Other"}}
%%html
<html>
<body>
<strong>This is %%HTML magic commands</strong>
</body>
</html>
This is %%HTML magic commands
Find the above post as jupyter notebook here: github
Find the above post as youtube video here: YouTube
...
Feedback is welcomed 💬