You can then just import it and use it as standard; for example, the following pops up an alert box if you run it from a notebook:
from IPython.core.magics.display
import Javascript
Javascript('alert("hello world")')
EDIT: To get the example you posted in the comments working, just wrap the Javascript you'd like to run in quotes and call it with Javascript
. Replacing In[4]
with this pops out the window as normal and should be fine to include in a normal Python function.
from IPython.core.magics.display import Javascript Javascript("" "$('div.inspector') .detach() .prependTo($('body')) .css({ 'z-index': 999, position: 'fixed', 'box-shadow': '5px 5px 12px -3px black', opacity: 0.9 }) .draggable(); "" ")
Magic commands act as convenient functions where Python syntax is not the most natural one. They are useful to embed invalid python syntax in their work flow.,Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.,This function converts current IPython history into an IPython notebook file with ipynb extension. The input cells in previous example are saved as sine.ipynb,This function also displays time required by IPython environment to execute a Python expression. Time execution of a Python statement or expression uses the timeit module. This function can be used both as a line and cell magic as explained here −
Built-in line magics
% autocall[mode]
This function activates matplotlib interactive support during an IPython session. However, it does not import matplotlib library. The matplotlib default GUI toolkit is TkAgg. But you can explicitly request a different GUI backend. You can see a list of the available backends as shown −
In[4]: % matplotlib--list
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg', 'gtk', 'tk', 'inline']
This function converts current IPython history into an IPython notebook file with ipynb extension. The input cells in previous example are saved as sine.ipynb
% notebook sine.ipynb
Updated: Nov 13, 2020,Nov 11, 20203 min read
% lsmagic
%run <file name>
def hello_world():
print('Hello, world')
hello_world()
%%time
<your code>
% % time
import random
for i in range(0, 1000000):
random.random()
Like normal Python functions, IPython magic functions have docstrings, and this useful documentation can be accessed in the standard manner. So, for example, to read the documentation of the %timeit magic simply type this:,Documentation for other functions can be accessed similarly. To access a general description of available magic functions, including some examples, you can type this:,These magic commands, like others we'll see, make available functionality that would be difficult or impossible in a standard Python interpreter.,Another example of a useful magic function is %timeit, which will automatically determine the execution time of the single-line Python statement that follows it. For example, we may want to check the performance of a list comprehension:
>>> def donothing(x):
...
return x
In [2]: >>> def donothing(x):
...: ... return x
...:
File "<ipython-input-20-5a66c8964687>", line 2
... return x
^
SyntaxError: invalid syntax
In[3]: % paste >>>
def donothing(x):
...
return x
# #--End pasted text--
In[4]: donothing(10)
Out[4]: 10
In[5]: % cpaste
Pasting code;
enter '--'
alone on the line to stop or use Ctrl - D.: >>> def donothing(x):
: ...
return x: --
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
# file: myscript.py
def square(x):
""
"square a number"
""
return x ** 2
for N in range(1, 4):
print(N, "squared is", square(N))
Jupyter notebook code cells can contain special commands which are not valid Python code but affect the behavior of the notebook. These special commands are called magic commands.,The %load command loads a Python module, webpage or file into a Jupyter notebook. If there is a file called hello.py in the same directory as the notebook with some Python code written in it, we can load that same code into a Jupyter notebook code cell with the %load command. ,You can list all of the available magic commands by typing and running %lsmagic in a Jupyter notebook code cell:,Within a Jupyter notebook code cell type the command:
One of the most popular magic commands is:
% matplotlib inline
Entering the %matplotlib inline
command at the top of a Jupyter notebook renders Matplotlib plots in cells of the notebook. Without %matplotlib inline
, plots may jump out as external windows. A typical start to a Jupyter notebook using Matplotlib might start as:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt %
matplotlib inline
Within a Jupyter notebook code cell type the command:
% load hello.py
This code was run from a seperate Python file
Hello from the file hello.py
If the %run
magic command followed by the name of a valid Python file, the Python file runs as a script. Suppose the file hello.py is created in the same directory as the running Jupyter notebook. The directory structure will look something like this:
| folder -- - | notebook.ipynb -- - | hello.py
# % load hello.py # hello.py print('This code was run from a seperate Python file') print('Hello from the file hello.py')
This code was run from a seperate Python file
Hello from the file hello.py
% run hello.py
This code was run from a separate Python file
Hello from the file hello.py
2021-05-12
% matplotlib inline
!pip install - q giphy - ipython - magic
% load_ext giphy_magic % giphy magic
!pip install - q cowsay
% % writefile. / cowsay_magic.py
import cowsay as cs
def cowsay(msg):
cs.cow(msg)
% matplotlib inline
!pip install - q giphy - ipython - magic
% load_ext giphy_magic % giphy magic
% % writefile. / cowsay_magic.py
import cowsay as cs
def cowsay(msg):
cs.cow(msg)
Overwriting. / cowsay_magic.py