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

Function cellmagic

IPython/core/inputtransformer.py:359–395  ·  view source on GitHub ↗

Captures & transforms cell magics. After a cell magic is started, this stores up any lines it gets until it is reset (sent None).

(end_on_blank_line=False)

Source from the content-addressed store, hash-verified

357
358@CoroutineInputTransformer.wrap
359def cellmagic(end_on_blank_line=False):
360 """Captures & transforms cell magics.
361
362 After a cell magic is started, this stores up any lines it gets until it is
363 reset (sent None).
364 """
365 tpl = 'get_ipython().run_cell_magic(%r, %r, %r)'
366 cellmagic_help_re = re.compile(r'%%\w+\?')
367 line = ''
368 while True:
369 line = (yield line)
370 # consume leading empty lines
371 while not line:
372 line = (yield line)
373
374 if not line.startswith(ESC_MAGIC2):
375 # This isn't a cell magic, idle waiting for reset then start over
376 while line is not None:
377 line = (yield line)
378 continue
379
380 if cellmagic_help_re.match(line):
381 # This case will be handled by help_end
382 continue
383
384 first = line
385 body = []
386 line = (yield None)
387 while (line is not None) and \
388 ((line.strip() != '') or not end_on_blank_line):
389 body.append(line)
390 line = (yield None)
391
392 # Output
393 magic_name, _, first = first.partition(' ')
394 magic_name = magic_name.lstrip(ESC_MAGIC2)
395 line = tpl % (magic_name, first, u'\n'.join(body))
396
397
398def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected