IPython Magic Commands in Jupyter Notebook (2026) - Part 7 | Step-by-Step Guide
Magic Commands¶
In Jupyter Notebook, Magic Commands are special built-in commands that provide additional functionality beyond normal Python commands.
They help users perform tasks such as file handling, timing code execution, managing variables, and controlling the notebook environment more easily.
Magic commands usually start with % for line magics and %% for cell magics.
They are designed to make working in Jupyter Notebook faster and more interactive.
1. %loadpy¶
In Jupyter Notebook, %loadpy is a magic command used to load the contents of a Python script (.py file) into a notebook cell.
It reads the source code from the specified Python file and inserts it into the current cell.
Useful when you want to reuse or edit code from an existing Python script within a notebook.
# %load my_script.py
def greet():
print("Hello, World!")
greet()
Hello, World!
2. %notebook¶
In Jupyter Notebook, %notebook is a magic command used to export the input history of the current IPython session to a Jupyter Notebook file (.ipynb).
It saves the commands and code cells that have been executed during the current session.
Useful for preserving your work and creating a notebook from an interactive IPython session.
The output notebook can later be opened and edited in Jupyter Notebook.
%notebook my_notebook.ipynb
3. %pastebin¶
In Jupyter Notebook/IPython, %pastebin is a magic command used to upload code from the input history to an online pastebin service.
It generates a shareable URL containing the selected code.
Useful for quickly sharing code snippets with others.
You can specify a range of input history lines to upload.
The availability of this command depends on internet access and the configured pastebin service.
%pastebin 1-10
'https://dpaste.com/C7S4959WX'
4. %set_env¶
In Jupyter Notebook/IPython, %set_env is a magic command used to set environment variables for the current session.
Environment variables can be accessed by Python programs and system commands.
Useful for storing configuration values such as API keys, file paths, or application settings.
The variables remain available only for the current notebook/kernel session unless set permanently in the operating system.
%set_env MY_VARIABLE=HelloWorld
env: MY_VARIABLE=HelloWorld
import os
print(os.environ["MY_VARIABLE"])
HelloWorld
%env
{'ALLUSERSPROFILE': 'C:\\ProgramData',
'APPDATA': 'C:\\Users\\schol\\AppData\\Roaming',
'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files',
'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files',
'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files',
'COMPUTERNAME': 'STFBONC',
'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe',
'CONDA_PREFIX': 'C:\\Users\\schol\\miniconda3\\envs\\jn',
'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData',
'EFC_10508_1262719628': '1',
'EFC_10508_1592913036': '1',
'EFC_10508_2283032206': '1',
'EFC_10508_2775293581': '1',
'EFC_10508_3789132940': '1',
'EFC_10508_4126798990': '1',
'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer',
'FPS_BROWSER_USER_PROFILE_STRING': 'Default',
'HOMEDRIVE': 'C:',
'HOMEPATH': '\\Users\\schol',
'IPY_INTERRUPT_EVENT': '3648',
'JPY_INTERRUPT_EVENT': '3648',
'JPY_PARENT_PID': '4996',
'JPY_SESSION_NAME': 'C:\\Users\\schol\\Projects\\Magic-Commands\\Magic-Commands-7.ipynb',
'LOCALAPPDATA': 'C:\\Users\\schol\\AppData\\Local',
'LOGONSERVER': '\\\\STFBONC',
'NUMBER_OF_PROCESSORS': '4',
'ONEDRIVE': 'C:\\Users\\schol\\OneDrive',
'OS': 'Windows_NT',
'PATH': 'C:\\Users\\schol\\miniconda3\\envs\\jn;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\mingw-w64\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\usr\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Library\\bin;C:\\Users\\schol\\miniconda3\\envs\\jn\\Scripts;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\schol\\miniconda3;C:\\Users\\schol\\miniconda3\\Library\\mingw-w64\\bin;C:\\Users\\schol\\miniconda3\\Library\\usr\\bin;C:\\Users\\schol\\miniconda3\\Library\\bin;C:\\Users\\schol\\miniconda3\\Scripts;C:\\Users\\schol\\AppData\\Local\\Microsoft\\WindowsApps',
'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
'PROCESSOR_ARCHITECTURE': 'AMD64',
'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 142 Stepping 12, GenuineIntel',
'PROCESSOR_LEVEL': '6',
'PROCESSOR_REVISION': '8e0c',
'PROGRAMDATA': 'C:\\ProgramData',
'PROGRAMFILES': 'C:\\Program Files',
'PROGRAMFILES(X86)': 'C:\\Program Files (x86)',
'PROGRAMW6432': 'C:\\Program Files',
'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules',
'PUBLIC': 'C:\\Users\\Public',
'SESSIONNAME': 'Console',
'SYSTEMDRIVE': 'C:',
'SYSTEMROOT': 'C:\\WINDOWS',
'TEMP': 'C:\\Users\\schol\\AppData\\Local\\Temp',
'TMP': 'C:\\Users\\schol\\AppData\\Local\\Temp',
'USERDOMAIN': 'STFBONC',
'USERDOMAIN_ROAMINGPROFILE': 'STFBONC',
'USERNAME': 'schol',
'USERPROFILE': 'C:\\Users\\schol',
'WINDIR': 'C:\\WINDOWS',
'ZES_ENABLE_SYSMAN': '1',
'PYDEVD_USE_FRAME_EVAL': 'NO',
'TERM': 'xterm-color',
'CLICOLOR': '1',
'FORCE_COLOR': '1',
'CLICOLOR_FORCE': '1',
'PAGER': 'cat',
'GIT_PAGER': 'cat',
'MPLBACKEND': 'module://matplotlib_inline.backend_inline',
'MY_VARIABLE': 'HelloWorld'}
5. %gui¶
In Jupyter Notebook/IPython, %gui is a magic command used to enable integration between IPython and a graphical user interface (GUI) event loop.
It allows GUI applications to run interactively without blocking the IPython shell.
Commonly used with GUI toolkits such as Tkinter, PyQt, PySide, GTK, and wxPython.
Helps create and test desktop GUI applications directly from a notebook or IPython session.
The specific GUI framework must be installed on your system.
%pip install PyQt5
Collecting PyQt5 Using cached PyQt5-5.15.11-cp38-abi3-win_amd64.whl.metadata (2.1 kB) Requirement already satisfied: PyQt5-sip<13,>=12.15 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from PyQt5) (12.18.0) Requirement already satisfied: PyQt5-Qt5<5.16.0,>=5.15.2 in C:\Users\schol\miniconda3\envs\jn\Lib\site-packages (from PyQt5) (5.15.2) Using cached PyQt5-5.15.11-cp38-abi3-win_amd64.whl (6.9 MB) Installing collected packages: PyQt5 Successfully installed PyQt5-5.15.11 Note: you may need to restart the kernel to use updated packages.
%gui qt
from PyQt5.QtWidgets import QApplication, QLabel
from PyQt5.QtGui import QFont
app = QApplication.instance() or QApplication([])
label = QLabel("Hello, Qt!")
label.setFont(QFont("Arial", 24))
label.resize(300, 100)
label.show()
6. %logstate¶
In IPython, %logstate is a magic command used to display the current status of session logging.
It shows whether logging is enabled or disabled.
Provides information about the log file being used and the logging mode.
Useful for checking the current logging configuration without changing it.
Often used together with %logstart, %logstop, and %logoff.
%logstate
Logging has not been activated.
7. %logstart¶
In IPython, %logstart is a magic command used to start logging the current session to a file.
It records commands entered during the session and saves them to a specified log file.
Useful for keeping a history of your work, debugging, or creating reusable scripts.
You can specify different logging modes such as append, backup, global, over, or rotate.
Logging continues until it is stopped with %logstop.
%logstart mylog.py
Activating auto-logging. Current session state plus future input saved. Filename : mylog.py Mode : backup Output logging : False Raw input log : False Timestamping : False State : active
%logstate
Filename : mylog.py Mode : backup Output logging : False Raw input log : False Timestamping : False State : active
8. %logstop¶
In IPython, %logstop is a magic command used to stop the current logging session.
It terminates logging that was previously started using %logstart.
After execution, commands entered in the session are no longer saved to the log file.
Useful when you want to stop recording your IPython activity without ending the session.
The log file created before stopping remains available for future reference.
%logstop
%logstate
Logging has not been activated.
9. %logoff¶
In IPython, %logoff is a magic command used to temporarily suspend logging without completely stopping the logging session.
It turns logging off while keeping the current log file open.
Useful when you want to exclude certain commands from being recorded.
Logging can be resumed later using %logon.
Unlike %logstop, it does not close the logging session.
%logstart mylog.py
Activating auto-logging. Current session state plus future input saved. Filename : mylog.py Mode : backup Output logging : False Raw input log : False Timestamping : False State : active
%logoff
Switching logging OFF
%logstate
Filename : mylog.py Mode : backup Output logging : False Raw input log : False Timestamping : False State : temporarily suspended
10. %logon¶
In IPython, %logon is a magic command used to resume logging after it has been temporarily turned off using %logoff.
It reactivates the existing logging session without creating a new log file.
Useful when you want to continue recording commands after a pause.
Works only if logging was previously started with %logstart.
It does not restart logging from scratch; it continues the same session.
%logon
Switching logging ON
%logstate
Filename : mylog.py Mode : backup Output logging : False Raw input log : False Timestamping : False State : active
Comments
Post a Comment