diff --git a/docs/source/config/intro.rst b/docs/source/config/intro.rst index d3ab800..af8bc79 100644 --- a/docs/source/config/intro.rst +++ b/docs/source/config/intro.rst @@ -112,6 +112,19 @@ At present, this only affects the current session - changes you make to config are not saved anywhere. Also, some options are only read when IPython starts, so they can't be changed like this. +.. _configure_start_ipython: + +Running IPython from Python +---------------------------- + +If you are using :ref:`embedding` to start IPython from a normal +python file, you can set configuration options the same way as in a +config file by creating a traitlets config object and passing it to +start_ipython like in the example below. + +.. literalinclude:: ../../../examples/Embedding/start_ipython_config.py + :language: python + .. _profiles: Profiles diff --git a/docs/source/interactive/reference.rst b/docs/source/interactive/reference.rst index e79d713..ef49565 100644 --- a/docs/source/interactive/reference.rst +++ b/docs/source/interactive/reference.rst @@ -604,6 +604,8 @@ You can start a regular IPython session with at any point in your program. This will load IPython configuration, startup files, and everything, just as if it were a normal IPython session. +For information on setting configuration options when running IPython from +python, see :ref:`configure_start_ipython`. It is also possible to embed an IPython shell in a namespace in your Python code. This allows you to evaluate dynamically the state of your code, diff --git a/examples/Embedding/start_ipython_config.py b/examples/Embedding/start_ipython_config.py new file mode 100755 index 0000000..f1fbaca --- /dev/null +++ b/examples/Embedding/start_ipython_config.py @@ -0,0 +1,22 @@ +"""Quick snippet explaining how to set config options when using start_ipython.""" + +# First create a config object from the traitlets library +from traitlets.config import Config +c = Config() + +# Now we can set options as we would in a config file: +# c.Class.config_value = value +# For example, we can set the exec_lines option of the InteractiveShellApp +# class to run some code when the IPython REPL starts +c.InteractiveShellApp.exec_lines = [ + 'print("\\nimporting some things\\n")', + 'import math', + "math" +] +c.InteractiveShell.colors = 'LightBG' +c.InteractiveShell.confirm_exit = False +c.TerminalIPythonApp.display_banner = False + +# Now we start ipython with our configuration +import IPython +IPython.start_ipython(config=c)