MCPcopy Create free account
hub / github.com/ipython/ipython / EmbeddedSphinxShell

Class EmbeddedSphinxShell

IPython/sphinxext/ipython_directive.py:330–878  ·  view source on GitHub ↗

An embedded IPython instance to run inside Sphinx

Source from the content-addressed store, hash-verified

328
329
330class EmbeddedSphinxShell(object):
331 """An embedded IPython instance to run inside Sphinx"""
332
333 def __init__(self, exec_lines=None):
334
335 self.cout = StringIO()
336
337 if exec_lines is None:
338 exec_lines = []
339
340 # Create config object for IPython
341 config = Config()
342 config.HistoryManager.hist_file = ':memory:'
343 config.InteractiveShell.autocall = False
344 config.InteractiveShell.autoindent = False
345 config.InteractiveShell.colors = 'NoColor'
346
347 # create a profile so instance history isn't saved
348 tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
349 profname = 'auto_profile_sphinx_build'
350 pdir = os.path.join(tmp_profile_dir,profname)
351 profile = ProfileDir.create_profile_dir(pdir)
352
353 # Create and initialize global ipython, but don't start its mainloop.
354 # This will persist across different EmbeddedSphinxShell instances.
355 IP = InteractiveShell.instance(config=config, profile_dir=profile)
356 atexit.register(self.cleanup)
357
358 # Store a few parts of IPython we'll need.
359 self.IP = IP
360 self.user_ns = self.IP.user_ns
361 self.user_global_ns = self.IP.user_global_ns
362
363 self.input = ''
364 self.output = ''
365 self.tmp_profile_dir = tmp_profile_dir
366
367 self.is_verbatim = False
368 self.is_doctest = False
369 self.is_suppress = False
370
371 # Optionally, provide more detailed information to shell.
372 # this is assigned by the SetUp method of IPythonDirective
373 # to point at itself.
374 #
375 # So, you can access handy things at self.directive.state
376 self.directive = None
377
378 # on the first call to the savefig decorator, we'll import
379 # pyplot as plt so we can make a call to the plt.gcf().savefig
380 self._pyplot_imported = False
381
382 # Prepopulate the namespace.
383 for line in exec_lines:
384 self.process_input_line(line, store_history=False)
385
386 def cleanup(self):
387 shutil.rmtree(self.tmp_profile_dir, ignore_errors=True)

Callers 1

setupMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected