Skip to content
This repository

Storemagic plugin #1073

Merged
merged 4 commits into from over 2 years ago

3 participants

Thomas Kluyver Fernando Perez Min RK
Thomas Kluyver
Collaborator

This makes the storemagic extension a plugin, so there's a configurable option for autorestoring variables on startup (default False).

It also adds a non-configurable default_extensions trait to InteractiveShellApp, for extensions we want to always load as part of IPython. I considered just having a default value for the extensions trait, but I think that's too easy to clobber unintentionally in config, e.g. c.InteractiveShellApp.extensions = ['sympyprinting'].

I think this is a shortcoming of the extension system, that there isn't a simple way to add values to a container - you have to do the little dance with hasattr, and then assign or use extend.

Thomas Kluyver
Collaborator

Actually, I'm not completely happy about this. Because extensions are loaded by the application, %store won't be available when it's embedded. I think the always-on extensions should be loaded by the shell.

Fernando Perez
Owner

Agreed, those should be shell-level. Do you want to do that here or in a different PR?

Min RK
Owner

Related: should extensions just be attached to the InteractiveShell in general, rather than the app? That seems to make more sense.

Fernando Perez
Owner

I agree, @minrk.

Min RK
Owner

Okay, then if we do move extensions to InteractiveShell from App, there should be a deprecation similar to the one we did for InteractiveShell.prompt_x.

Min RK
Owner

opened #1144 for moving extensions to Shell, merging this.

Min RK minrk merged commit d3ded1a into from
Min RK minrk closed this
Brian E. Granger ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
7  IPython/core/shellapp.py
@@ -130,6 +130,9 @@ def _extra_extension_changed(self, name, old, new):
130 130
         if new:
131 131
             # add to self.extensions
132 132
             self.extensions.append(new)
  133
+    
  134
+    # Extensions that are always loaded (not configurable)
  135
+    default_extensions = List(Unicode, [u'storemagic'], config=False)
133 136
 
134 137
     exec_files = List(Unicode, config=True,
135 138
         help="""List of files to run at IPython startup."""
@@ -158,11 +161,9 @@ def init_extensions(self):
158 161
         This uses the :meth:`ExtensionManager.load_extensions` to load all
159 162
         the extensions listed in ``self.extensions``.
160 163
         """
161  
-        if not self.extensions:
162  
-            return
163 164
         try:
164 165
             self.log.debug("Loading IPython extensions...")
165  
-            extensions = self.extensions
  166
+            extensions = self.default_extensions + self.extensions
166 167
             for ext in extensions:
167 168
                 try:
168 169
                     self.log.info("Loading IPython extension: %s" % ext)
41  IPython/extensions/storemagic.py
@@ -2,26 +2,24 @@
2 2
 """
3 3
 %store magic for lightweight persistence.
4 4
 
5  
-Stores variables, aliases and macros in IPython's database. Stored values will
6  
-be automatically restored whenever the extension is loaded.
  5
+Stores variables, aliases and macros in IPython's database.
7 6
 
8  
-To enable this functionality, list it in your default profile
9  
-`ipython_config.py` file::
  7
+To automatically restore stored variables at startup, add this to your
  8
+:file:`ipython_config.py` file::
10 9
 
11  
-  c.InteractiveShellApp.extensions = ['storemagic']
12  
-
13  
-Or to use it temporarily, run this in your IPython session::
14  
-
15  
-  %load_ext storemagic
  10
+  c.StoreMagic.autorestore = True
16 11
 
17 12
 """
18 13
 
19 14
 from IPython.core.error import TryNext, UsageError
  15
+from IPython.core.plugin import Plugin
  16
+from IPython.testing.skipdoctest import skip_doctest
20 17
 from IPython.utils import pickleshare
  18
+from IPython.utils.traitlets import Bool, Instance
21 19
 
22 20
 import inspect,pickle,os,sys,textwrap
23 21
 from IPython.core.fakemodule import FakeModule
24  
-
  22
+    
25 23
 def restore_aliases(ip):
26 24
     staliases = ip.db.get('stored_aliases', {})
27 25
     for k,v in staliases.items():
@@ -53,6 +51,7 @@ def restore_data(ip):
53 51
     restore_aliases(ip)
54 52
     restore_dhist(ip)
55 53
 
  54
+@skip_doctest
56 55
 def magic_store(self, parameter_s=''):
57 56
     """Lightweight persistence for python variables.
58 57
 
@@ -183,6 +182,24 @@ def magic_store(self, parameter_s=''):
183 182
             self.db[ 'autorestore/' + args[0] ] = obj
184 183
             print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
185 184
 
  185
+
  186
+class StoreMagic(Plugin):
  187
+    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
  188
+    autorestore = Bool(False, config=True)
  189
+    
  190
+    def __init__(self, shell, config):
  191
+        super(StoreMagic, self).__init__(shell=shell, config=config)
  192
+        shell.define_magic('store', magic_store)
  193
+        
  194
+        if self.autorestore:
  195
+            restore_data(shell)
  196
+
  197
+_loaded = False
  198
+
186 199
 def load_ipython_extension(ip):
187  
-    ip.define_magic('store', magic_store)
188  
-    restore_data(ip)
  200
+    """Load the extension in IPython."""
  201
+    global _loaded
  202
+    if not _loaded:
  203
+        plugin = StoreMagic(shell=ip, config=ip.config)
  204
+        ip.plugin_manager.register_plugin('storemagic', plugin)
  205
+        _loaded = True
5  docs/source/whatsnew/development.txt
@@ -62,8 +62,9 @@ New features
62 62
   Terminal frontend by default (:ghpull:`838`).
63 63
 
64 64
 * **%store**: The ``%store`` magic from earlier versions has been updated and
65  
-  placed in an extension, :ref:`extensions_storemagic`. Add 'storemagic' to ``c.InteractiveShellApp.extensions``
66  
-  in ipython_config.py to enable it (:ghpull:`1029`).
  65
+  re-enabled (:ref:`extensions_storemagic`; :ghpull:`1029`). To autorestore
  66
+  stored variables on startup, specify ``c.StoreMagic.autorestore = True`` in
  67
+  :file:`ipython_config.py`.
67 68
 
68 69
 
69 70
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.