Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython - set up magic commands in configuration file

I use iPython mostly via notebooks but also in the terminal. I just created my default profile by running ipython profile create.

I can't seem to figure out how to have the profile run several magic commands that I use every time. I tried to look this up online and in a book I'm reading but can't get it to work. For example, if I want %debug activated for every new notebook I tried adding these lines to my config file:

c.InteractiveShellApp.extensions = ['debug']

or

c.TerminalPythonApp.extensions = ['debug']

and I either get import errors or nothing. My (closely related) questions are the following:

  1. What line to do I add to my ipython config file to activate magic commands? Some require parameters, e.g. %reload_ext autoreload and %autoreload 2. How do I also pass these parameters in the config file?

  2. Can I separate which get added for terminal vs. notebooks in a single config file or must I set up separate profiles if I want different magic's activated? (e.g., matplotlib inline or not). Do the two lines above affect notebooks vs. terminal settings (i.e., c.InteractiveShellApp vs. c.TerminalPythonApp)?

Thank you!

like image 251
kilgoretrout Avatar asked Sep 20 '15 15:09

kilgoretrout


People also ask

Where is IPython configuration file?

IPython stores its files—config, command history and extensions—in the directory ~/. ipython/ by default. If set, this environment variable should be the path to a directory, which IPython will use for user data.

How do you get all magic commands?

As a first example, you can use the magic command %lsmagic to list the available magic commands (Figure 3.11). To get the output you have to execute the cell as with any other code cell. The %load_ext magic command can be used for loading IPython extension which can add new magic commands.

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do I create a config file in Jupyter Notebook?

To create a jupyter_notebook_config.py file in the . jupyter directory, with all the defaults commented out, use the following command: $ jupyter notebook --generate-config :ref:`Command line arguments for configuration <config>` settings are documented in the configuration file and the user documentation.

What is Magic command in IPython?

IPython - Magic Commands. 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.

How do I create a magic in Python?

You can also create magics of all three kinds by inheriting from the IPython.core.magic.Magics class. This lets you create magics that can potentially hold state in between calls, and that have full access to the main IPython object:

How to configure a class in IPython?

Run the conda package manager within the current kernel. This magic exposes most of the IPython config system. Any Configurable class should be able to be configured with the simple line: Where value will be resolved in the user’s namespace, if it is an expression or variable name.

Is it possible to enable GUI toolkit in IPython?

This magic replaces IPython’s threaded shells that were activated using the (pylab/wthread/etc.) command line flags. GUI toolkits can now be enabled at runtime and keyboard interrupts should work without any problems.


Video Answer


2 Answers

Execute magics as follows:

get_ipython().magic(u"%reload_ext autoreload")
get_ipython().magic(u"%autoreload 2")

You can put those lines in your startup script here:

~/.ipython/profile_default/startup/00-first.py
like image 132
shad Avatar answered Oct 09 '22 23:10

shad


To start for example the %pylab magic command on startup do the following:

ipython profile create pylab

Add the following code to your .ipython\profile_pylab\ipython_config.py

c.InteractiveShellApp.exec_lines = ['%pylab']

and start ipython

ipython --profile=pylab
like image 1
robotrovsky Avatar answered Oct 10 '22 00:10

robotrovsky