Close Navigation
Learn more about IBKR accounts
Jupyter Notebook Tutorial: Installation, Components and Magic Commands – Part IV

Jupyter Notebook Tutorial: Installation, Components and Magic Commands – Part IV

Posted February 23, 2023
Jay Parmar
QuantInsti

See Part I for an overview of Jupyter Notebook, Part II for the different components of Jupyter Notebooks, and Part III for instructions on how to write down in Markdown in Jupyter Notebooks.

Images

The format to insert an image in a markdown cell is very similar to that of hyperlinks. Only the difference is that it prefixes ! before the content. First, the exclamation mark followed by the name of the image in square brackets, and finally URL in the parenthesis. This is shown below:

![QuantInsti's Logo](https://dt99qig9iutro.cloudfront.net/production/images/header-logo-green.png)

Alternatively, you can use the HTML image tag to insert an image as shown below:

<img src="https://dt99qig9iutro.cloudfront.net/production/images/header-logo-green.png" alt="QuantInsti's Logo">

Both will produce the following result:

Geometric Shapes

A UTF-8 Geometric Shape can be included in a Jupyter notebook by using its decimal reference number. Use the following format to insert any of the shapes.

&#reference_number;

A black circle can be inserted, as shown below:

Programming Code

You can embed programming code within the text using triple backticks followed by programming language name and ending by triple backticks. Below is the example for Python code embedded within the text.

```python
def func(x):
 return x**2
```

Similarly, the programming code for another language can be written. For example, below code is for Javascript.

```javascript
console.log('Hello World!')
```

LaTeX Equations

By courtesy of MathJax, you can include mathematical expressions either inline or separately in markdown cells. To type inline, equations are surrounded by $. To print equations separately on a new line, they are surrounded by $$. For example, the formula to calculate the mean value can be written as

$$\mu = \frac{1}{n}\sum_{i=1}^{n} x_i $$

which results in the following:

Inline equations can be written as $x_t = \phi x_{t-1} + \epsilon_t$ which results in

You can refer to this thread which lists various LaTeX commands that can be used in markdown cells.

Tables

You can create tables in markdown cells, as shown below:

|This|is|
|-|-|
|a|table|

Columns are separated by vertical bar | and rows are written in a new line. The above table is generated, as shown below:

Thisis
atable

What are the magic commands in Jupyter Notebook?

Jupyter notebook software comes with a bunch of built-in commands which adds interactivity while working with it. They are called magic commands in Jupyter environment. These commands are dependent on the interpreter or kernel with which you are working. To see which magic commands are available, you can run the following magic command in the code cell:

%lsmagic

These magic commands are prefixed with the % value. Following are some of the commonly used magic commands along with their functionality:

  • %autosave command autosaves your notebook periodically.
# Save notebook every 60 seconds
%autosave 60
  • %cd changes the current working directory to the new directory given as an argument.
  • %clear and %cls command clear the output of the current cell.
  • %envallows you to list all environment variables as well as set the value of particular environment variables.
  • %history shows the history of the previously executed magic commands.
%history

%ls lists the content of the current directory.

%ls

%matplotlib allows you to plot charts inline within a notebook.

%matplotlib inline
  • %notebook command adds the interactivity while plotting charts.
  • %pdb allows you to debug the code. This magic command is the Jupyter version of Python debugger.
  • %pip enables you to list and install all available packages from Jupyter environment.
%pip install pandas

%who list all variables from the global scope.

stock = 'AAPL'
price = 222.15
%who str

%load inserts the code from an external script.

%load ./hello_world.py

%run allows you to run Python code.

%run ./sample_notebook.ipynb
  • %time displays the time taken by a cell for execution.

If you are using Anaconda Python, then magic commands mentioned above should work without any issue. Otherwise, you may require to install the following packages:

  • ipython-sql
  • cython

Stay tuned for the next installment to learn how to download and share Jupyter Notebook.

Originally posted on QuantInsti Blog: https://blog.quantinsti.com/jupyter-notebook-tutorial-installation-components-magic-commands/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.