1+ from textwrap import dedent
2+
13from .convenience import with_message
24from .core import get_main
35
46
7+ _done = False
8+
9+ def _run_once (ip ):
10+ global _done
11+ if not _done :
12+ _done = True
13+ post_startup_configuration (ip )
14+
15+
16+ def post_startup_configuration (ip ):
17+ from . import ipyext
18+ ip .run_cell (dedent ("""
19+ %load_ext {ipyext}
20+ """ .format (ipyext = ipyext .__name__ )))
21+
22+
523def ipython_options (** kwargs ):
624 from traitlets .config import Config
725
@@ -14,10 +32,19 @@ def ipython_options(**kwargs):
1432 c .TerminalIPythonApp .matplotlib = None # don't close figures
1533 c .TerminalInteractiveShell .confirm_exit = False
1634
17- from . import ipyext
18- c .InteractiveShellApp .extensions = [
19- ipyext .__name__ ,
20- ]
35+ # "-i -c <code_to_run>"
36+ c .InteractiveShellApp .code_to_run = """
37+ __import__({!r}).{}._run_once(get_ipython())
38+ """ .strip ().format (__name__ , __name__ .split ("." , 1 )[- 1 ])
39+ c .InteractiveShellApp .force_interact = True # "-i"
40+ #
41+ # To not override user's `c.InteractiveShellApp.extensions`
42+ # setting, use `c.InteractiveShellApp.code_to_run` to load our
43+ # extension. This is equivalent to passing "-c" option to ipython
44+ # CLI so it is likely to be not configured in user's IPython
45+ # profile. However, `code_to_run` will be invoked every time
46+ # re-entering to IPython which prints "extension is already
47+ # loaded" warning. `_run_once` wrapper is used to avoid that.
2148
2249 return dict (user_ns = user_ns , config = c )
2350
0 commit comments