demo.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. """Module for interactive demos using IPython.
  2. This module implements a few classes for running Python scripts interactively
  3. in IPython for demonstrations. With very simple markup (a few tags in
  4. comments), you can control points where the script stops executing and returns
  5. control to IPython.
  6. Provided classes
  7. ----------------
  8. The classes are (see their docstrings for further details):
  9. - Demo: pure python demos
  10. - IPythonDemo: demos with input to be processed by IPython as if it had been
  11. typed interactively (so magics work, as well as any other special syntax you
  12. may have added via input prefilters).
  13. - LineDemo: single-line version of the Demo class. These demos are executed
  14. one line at a time, and require no markup.
  15. - IPythonLineDemo: IPython version of the LineDemo class (the demo is
  16. executed a line at a time, but processed via IPython).
  17. - ClearMixin: mixin to make Demo classes with less visual clutter. It
  18. declares an empty marquee and a pre_cmd that clears the screen before each
  19. block (see Subclassing below).
  20. - ClearDemo, ClearIPDemo: mixin-enabled versions of the Demo and IPythonDemo
  21. classes.
  22. Inheritance diagram:
  23. .. inheritance-diagram:: IPython.lib.demo
  24. :parts: 3
  25. Subclassing
  26. -----------
  27. The classes here all include a few methods meant to make customization by
  28. subclassing more convenient. Their docstrings below have some more details:
  29. - highlight(): format every block and optionally highlight comments and
  30. docstring content.
  31. - marquee(): generates a marquee to provide visible on-screen markers at each
  32. block start and end.
  33. - pre_cmd(): run right before the execution of each block.
  34. - post_cmd(): run right after the execution of each block. If the block
  35. raises an exception, this is NOT called.
  36. Operation
  37. ---------
  38. The file is run in its own empty namespace (though you can pass it a string of
  39. arguments as if in a command line environment, and it will see those as
  40. sys.argv). But at each stop, the global IPython namespace is updated with the
  41. current internal demo namespace, so you can work interactively with the data
  42. accumulated so far.
  43. By default, each block of code is printed (with syntax highlighting) before
  44. executing it and you have to confirm execution. This is intended to show the
  45. code to an audience first so you can discuss it, and only proceed with
  46. execution once you agree. There are a few tags which allow you to modify this
  47. behavior.
  48. The supported tags are:
  49. # <demo> stop
  50. Defines block boundaries, the points where IPython stops execution of the
  51. file and returns to the interactive prompt.
  52. You can optionally mark the stop tag with extra dashes before and after the
  53. word 'stop', to help visually distinguish the blocks in a text editor:
  54. # <demo> --- stop ---
  55. # <demo> silent
  56. Make a block execute silently (and hence automatically). Typically used in
  57. cases where you have some boilerplate or initialization code which you need
  58. executed but do not want to be seen in the demo.
  59. # <demo> auto
  60. Make a block execute automatically, but still being printed. Useful for
  61. simple code which does not warrant discussion, since it avoids the extra
  62. manual confirmation.
  63. # <demo> auto_all
  64. This tag can _only_ be in the first block, and if given it overrides the
  65. individual auto tags to make the whole demo fully automatic (no block asks
  66. for confirmation). It can also be given at creation time (or the attribute
  67. set later) to override what's in the file.
  68. While _any_ python file can be run as a Demo instance, if there are no stop
  69. tags the whole file will run in a single block (no different that calling
  70. first %pycat and then %run). The minimal markup to make this useful is to
  71. place a set of stop tags; the other tags are only there to let you fine-tune
  72. the execution.
  73. This is probably best explained with the simple example file below. You can
  74. copy this into a file named ex_demo.py, and try running it via::
  75. from IPython.lib.demo import Demo
  76. d = Demo('ex_demo.py')
  77. d()
  78. Each time you call the demo object, it runs the next block. The demo object
  79. has a few useful methods for navigation, like again(), edit(), jump(), seek()
  80. and back(). It can be reset for a new run via reset() or reloaded from disk
  81. (in case you've edited the source) via reload(). See their docstrings below.
  82. Note: To make this simpler to explore, a file called "demo-exercizer.py" has
  83. been added to the "docs/examples/core" directory. Just cd to this directory in
  84. an IPython session, and type::
  85. %run demo-exercizer.py
  86. and then follow the directions.
  87. Example
  88. -------
  89. The following is a very simple example of a valid demo file.
  90. ::
  91. #################### EXAMPLE DEMO <ex_demo.py> ###############################
  92. '''A simple interactive demo to illustrate the use of IPython's Demo class.'''
  93. print('Hello, welcome to an interactive IPython demo.')
  94. # The mark below defines a block boundary, which is a point where IPython will
  95. # stop execution and return to the interactive prompt. The dashes are actually
  96. # optional and used only as a visual aid to clearly separate blocks while
  97. # editing the demo code.
  98. # <demo> stop
  99. x = 1
  100. y = 2
  101. # <demo> stop
  102. # the mark below makes this block as silent
  103. # <demo> silent
  104. print('This is a silent block, which gets executed but not printed.')
  105. # <demo> stop
  106. # <demo> auto
  107. print('This is an automatic block.')
  108. print('It is executed without asking for confirmation, but printed.')
  109. z = x + y
  110. print('z =', x)
  111. # <demo> stop
  112. # This is just another normal block.
  113. print('z is now:', z)
  114. print('bye!')
  115. ################### END EXAMPLE DEMO <ex_demo.py> ############################
  116. """
  117. #*****************************************************************************
  118. # Copyright (C) 2005-2006 Fernando Perez. <Fernando.Perez@colorado.edu>
  119. #
  120. # Distributed under the terms of the BSD License. The full license is in
  121. # the file COPYING, distributed as part of this software.
  122. #
  123. #*****************************************************************************
  124. import os
  125. import re
  126. import shlex
  127. import sys
  128. import pygments
  129. from pathlib import Path
  130. from IPython.utils.text import marquee
  131. from IPython.utils import openpy
  132. from IPython.utils import py3compat
  133. __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError']
  134. class DemoError(Exception): pass
  135. def re_mark(mark):
  136. return re.compile(r'^\s*#\s+<demo>\s+%s\s*$' % mark,re.MULTILINE)
  137. class Demo(object):
  138. re_stop = re_mark(r'-*\s?stop\s?-*')
  139. re_silent = re_mark('silent')
  140. re_auto = re_mark('auto')
  141. re_auto_all = re_mark('auto_all')
  142. def __init__(self,src,title='',arg_str='',auto_all=None, format_rst=False,
  143. formatter='terminal', style='default'):
  144. """Make a new demo object. To run the demo, simply call the object.
  145. See the module docstring for full details and an example (you can use
  146. IPython.Demo? in IPython to see it).
  147. Inputs:
  148. - src is either a file, or file-like object, or a
  149. string that can be resolved to a filename.
  150. Optional inputs:
  151. - title: a string to use as the demo name. Of most use when the demo
  152. you are making comes from an object that has no filename, or if you
  153. want an alternate denotation distinct from the filename.
  154. - arg_str(''): a string of arguments, internally converted to a list
  155. just like sys.argv, so the demo script can see a similar
  156. environment.
  157. - auto_all(None): global flag to run all blocks automatically without
  158. confirmation. This attribute overrides the block-level tags and
  159. applies to the whole demo. It is an attribute of the object, and
  160. can be changed at runtime simply by reassigning it to a boolean
  161. value.
  162. - format_rst(False): a bool to enable comments and doc strings
  163. formatting with pygments rst lexer
  164. - formatter('terminal'): a string of pygments formatter name to be
  165. used. Useful values for terminals: terminal, terminal256,
  166. terminal16m
  167. - style('default'): a string of pygments style name to be used.
  168. """
  169. if hasattr(src, "read"):
  170. # It seems to be a file or a file-like object
  171. self.fname = "from a file-like object"
  172. if title == '':
  173. self.title = "from a file-like object"
  174. else:
  175. self.title = title
  176. else:
  177. # Assume it's a string or something that can be converted to one
  178. self.fname = src
  179. if title == '':
  180. (filepath, filename) = os.path.split(src)
  181. self.title = filename
  182. else:
  183. self.title = title
  184. self.sys_argv = [src] + shlex.split(arg_str)
  185. self.auto_all = auto_all
  186. self.src = src
  187. try:
  188. ip = get_ipython() # this is in builtins whenever IPython is running
  189. self.inside_ipython = True
  190. except NameError:
  191. self.inside_ipython = False
  192. if self.inside_ipython:
  193. # get a few things from ipython. While it's a bit ugly design-wise,
  194. # it ensures that things like color scheme and the like are always in
  195. # sync with the ipython mode being used. This class is only meant to
  196. # be used inside ipython anyways, so it's OK.
  197. self.ip_ns = ip.user_ns
  198. self.ip_colorize = ip.pycolorize
  199. self.ip_showtb = ip.showtraceback
  200. self.ip_run_cell = ip.run_cell
  201. self.shell = ip
  202. self.formatter = pygments.formatters.get_formatter_by_name(formatter,
  203. style=style)
  204. self.python_lexer = pygments.lexers.get_lexer_by_name("py3")
  205. self.format_rst = format_rst
  206. if format_rst:
  207. self.rst_lexer = pygments.lexers.get_lexer_by_name("rst")
  208. # load user data and initialize data structures
  209. self.reload()
  210. def fload(self):
  211. """Load file object."""
  212. # read data and parse into blocks
  213. if hasattr(self, 'fobj') and self.fobj is not None:
  214. self.fobj.close()
  215. if hasattr(self.src, "read"):
  216. # It seems to be a file or a file-like object
  217. self.fobj = self.src
  218. else:
  219. # Assume it's a string or something that can be converted to one
  220. self.fobj = openpy.open(self.fname)
  221. def reload(self):
  222. """Reload source from disk and initialize state."""
  223. self.fload()
  224. self.src = "".join(openpy.strip_encoding_cookie(self.fobj))
  225. src_b = [b.strip() for b in self.re_stop.split(self.src) if b]
  226. self._silent = [bool(self.re_silent.findall(b)) for b in src_b]
  227. self._auto = [bool(self.re_auto.findall(b)) for b in src_b]
  228. # if auto_all is not given (def. None), we read it from the file
  229. if self.auto_all is None:
  230. self.auto_all = bool(self.re_auto_all.findall(src_b[0]))
  231. else:
  232. self.auto_all = bool(self.auto_all)
  233. # Clean the sources from all markup so it doesn't get displayed when
  234. # running the demo
  235. src_blocks = []
  236. auto_strip = lambda s: self.re_auto.sub('',s)
  237. for i,b in enumerate(src_b):
  238. if self._auto[i]:
  239. src_blocks.append(auto_strip(b))
  240. else:
  241. src_blocks.append(b)
  242. # remove the auto_all marker
  243. src_blocks[0] = self.re_auto_all.sub('',src_blocks[0])
  244. self.nblocks = len(src_blocks)
  245. self.src_blocks = src_blocks
  246. # also build syntax-highlighted source
  247. self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
  248. # ensure clean namespace and seek offset
  249. self.reset()
  250. def reset(self):
  251. """Reset the namespace and seek pointer to restart the demo"""
  252. self.user_ns = {}
  253. self.finished = False
  254. self.block_index = 0
  255. def _validate_index(self,index):
  256. if index<0 or index>=self.nblocks:
  257. raise ValueError('invalid block index %s' % index)
  258. def _get_index(self,index):
  259. """Get the current block index, validating and checking status.
  260. Returns None if the demo is finished"""
  261. if index is None:
  262. if self.finished:
  263. print('Demo finished. Use <demo_name>.reset() if you want to rerun it.')
  264. return None
  265. index = self.block_index
  266. else:
  267. self._validate_index(index)
  268. return index
  269. def seek(self,index):
  270. """Move the current seek pointer to the given block.
  271. You can use negative indices to seek from the end, with identical
  272. semantics to those of Python lists."""
  273. if index<0:
  274. index = self.nblocks + index
  275. self._validate_index(index)
  276. self.block_index = index
  277. self.finished = False
  278. def back(self,num=1):
  279. """Move the seek pointer back num blocks (default is 1)."""
  280. self.seek(self.block_index-num)
  281. def jump(self,num=1):
  282. """Jump a given number of blocks relative to the current one.
  283. The offset can be positive or negative, defaults to 1."""
  284. self.seek(self.block_index+num)
  285. def again(self):
  286. """Move the seek pointer back one block and re-execute."""
  287. self.back(1)
  288. self()
  289. def edit(self,index=None):
  290. """Edit a block.
  291. If no number is given, use the last block executed.
  292. This edits the in-memory copy of the demo, it does NOT modify the
  293. original source file. If you want to do that, simply open the file in
  294. an editor and use reload() when you make changes to the file. This
  295. method is meant to let you change a block during a demonstration for
  296. explanatory purposes, without damaging your original script."""
  297. index = self._get_index(index)
  298. if index is None:
  299. return
  300. # decrease the index by one (unless we're at the very beginning), so
  301. # that the default demo.edit() call opens up the sblock we've last run
  302. if index>0:
  303. index -= 1
  304. filename = self.shell.mktempfile(self.src_blocks[index])
  305. self.shell.hooks.editor(filename, 1)
  306. with open(Path(filename), "r", encoding="utf-8") as f:
  307. new_block = f.read()
  308. # update the source and colored block
  309. self.src_blocks[index] = new_block
  310. self.src_blocks_colored[index] = self.highlight(new_block)
  311. self.block_index = index
  312. # call to run with the newly edited index
  313. self()
  314. def show(self,index=None):
  315. """Show a single block on screen"""
  316. index = self._get_index(index)
  317. if index is None:
  318. return
  319. print(self.marquee('<%s> block # %s (%s remaining)' %
  320. (self.title,index,self.nblocks-index-1)))
  321. print(self.src_blocks_colored[index])
  322. sys.stdout.flush()
  323. def show_all(self):
  324. """Show entire demo on screen, block by block"""
  325. fname = self.title
  326. title = self.title
  327. nblocks = self.nblocks
  328. silent = self._silent
  329. marquee = self.marquee
  330. for index,block in enumerate(self.src_blocks_colored):
  331. if silent[index]:
  332. print(marquee('<%s> SILENT block # %s (%s remaining)' %
  333. (title,index,nblocks-index-1)))
  334. else:
  335. print(marquee('<%s> block # %s (%s remaining)' %
  336. (title,index,nblocks-index-1)))
  337. print(block, end=' ')
  338. sys.stdout.flush()
  339. def run_cell(self,source):
  340. """Execute a string with one or more lines of code"""
  341. exec(source, self.user_ns)
  342. def __call__(self,index=None):
  343. """run a block of the demo.
  344. If index is given, it should be an integer >=1 and <= nblocks. This
  345. means that the calling convention is one off from typical Python
  346. lists. The reason for the inconsistency is that the demo always
  347. prints 'Block n/N, and N is the total, so it would be very odd to use
  348. zero-indexing here."""
  349. index = self._get_index(index)
  350. if index is None:
  351. return
  352. try:
  353. marquee = self.marquee
  354. next_block = self.src_blocks[index]
  355. self.block_index += 1
  356. if self._silent[index]:
  357. print(marquee('Executing silent block # %s (%s remaining)' %
  358. (index,self.nblocks-index-1)))
  359. else:
  360. self.pre_cmd()
  361. self.show(index)
  362. if self.auto_all or self._auto[index]:
  363. print(marquee('output:'))
  364. else:
  365. print(marquee('Press <q> to quit, <Enter> to execute...'), end=' ')
  366. ans = py3compat.input().strip()
  367. if ans:
  368. print(marquee('Block NOT executed'))
  369. return
  370. try:
  371. save_argv = sys.argv
  372. sys.argv = self.sys_argv
  373. self.run_cell(next_block)
  374. self.post_cmd()
  375. finally:
  376. sys.argv = save_argv
  377. except:
  378. if self.inside_ipython:
  379. self.ip_showtb(filename=self.fname)
  380. else:
  381. if self.inside_ipython:
  382. self.ip_ns.update(self.user_ns)
  383. if self.block_index == self.nblocks:
  384. mq1 = self.marquee('END OF DEMO')
  385. if mq1:
  386. # avoid spurious print if empty marquees are used
  387. print()
  388. print(mq1)
  389. print(self.marquee('Use <demo_name>.reset() if you want to rerun it.'))
  390. self.finished = True
  391. # These methods are meant to be overridden by subclasses who may wish to
  392. # customize the behavior of of their demos.
  393. def marquee(self,txt='',width=78,mark='*'):
  394. """Return the input string centered in a 'marquee'."""
  395. return marquee(txt,width,mark)
  396. def pre_cmd(self):
  397. """Method called before executing each block."""
  398. pass
  399. def post_cmd(self):
  400. """Method called after executing each block."""
  401. pass
  402. def highlight(self, block):
  403. """Method called on each block to highlight it content"""
  404. tokens = pygments.lex(block, self.python_lexer)
  405. if self.format_rst:
  406. from pygments.token import Token
  407. toks = []
  408. for token in tokens:
  409. if token[0] == Token.String.Doc and len(token[1]) > 6:
  410. toks += pygments.lex(token[1][:3], self.python_lexer)
  411. # parse doc string content by rst lexer
  412. toks += pygments.lex(token[1][3:-3], self.rst_lexer)
  413. toks += pygments.lex(token[1][-3:], self.python_lexer)
  414. elif token[0] == Token.Comment.Single:
  415. toks.append((Token.Comment.Single, token[1][0]))
  416. # parse comment content by rst lexer
  417. # remove the extra newline added by rst lexer
  418. toks += list(pygments.lex(token[1][1:], self.rst_lexer))[:-1]
  419. else:
  420. toks.append(token)
  421. tokens = toks
  422. return pygments.format(tokens, self.formatter)
  423. class IPythonDemo(Demo):
  424. """Class for interactive demos with IPython's input processing applied.
  425. This subclasses Demo, but instead of executing each block by the Python
  426. interpreter (via exec), it actually calls IPython on it, so that any input
  427. filters which may be in place are applied to the input block.
  428. If you have an interactive environment which exposes special input
  429. processing, you can use this class instead to write demo scripts which
  430. operate exactly as if you had typed them interactively. The default Demo
  431. class requires the input to be valid, pure Python code.
  432. """
  433. def run_cell(self,source):
  434. """Execute a string with one or more lines of code"""
  435. self.shell.run_cell(source)
  436. class LineDemo(Demo):
  437. """Demo where each line is executed as a separate block.
  438. The input script should be valid Python code.
  439. This class doesn't require any markup at all, and it's meant for simple
  440. scripts (with no nesting or any kind of indentation) which consist of
  441. multiple lines of input to be executed, one at a time, as if they had been
  442. typed in the interactive prompt.
  443. Note: the input can not have *any* indentation, which means that only
  444. single-lines of input are accepted, not even function definitions are
  445. valid."""
  446. def reload(self):
  447. """Reload source from disk and initialize state."""
  448. # read data and parse into blocks
  449. self.fload()
  450. lines = self.fobj.readlines()
  451. src_b = [l for l in lines if l.strip()]
  452. nblocks = len(src_b)
  453. self.src = ''.join(lines)
  454. self._silent = [False]*nblocks
  455. self._auto = [True]*nblocks
  456. self.auto_all = True
  457. self.nblocks = nblocks
  458. self.src_blocks = src_b
  459. # also build syntax-highlighted source
  460. self.src_blocks_colored = list(map(self.highlight,self.src_blocks))
  461. # ensure clean namespace and seek offset
  462. self.reset()
  463. class IPythonLineDemo(IPythonDemo,LineDemo):
  464. """Variant of the LineDemo class whose input is processed by IPython."""
  465. pass
  466. class ClearMixin(object):
  467. """Use this mixin to make Demo classes with less visual clutter.
  468. Demos using this mixin will clear the screen before every block and use
  469. blank marquees.
  470. Note that in order for the methods defined here to actually override those
  471. of the classes it's mixed with, it must go /first/ in the inheritance
  472. tree. For example:
  473. class ClearIPDemo(ClearMixin,IPythonDemo): pass
  474. will provide an IPythonDemo class with the mixin's features.
  475. """
  476. def marquee(self,txt='',width=78,mark='*'):
  477. """Blank marquee that returns '' no matter what the input."""
  478. return ''
  479. def pre_cmd(self):
  480. """Method called before executing each block.
  481. This one simply clears the screen."""
  482. from IPython.utils.terminal import _term_clear
  483. _term_clear()
  484. class ClearDemo(ClearMixin,Demo):
  485. pass
  486. class ClearIPDemo(ClearMixin,IPythonDemo):
  487. pass
  488. def slide(file_path, noclear=False, format_rst=True, formatter="terminal",
  489. style="native", auto_all=False, delimiter='...'):
  490. if noclear:
  491. demo_class = Demo
  492. else:
  493. demo_class = ClearDemo
  494. demo = demo_class(file_path, format_rst=format_rst, formatter=formatter,
  495. style=style, auto_all=auto_all)
  496. while not demo.finished:
  497. demo()
  498. try:
  499. py3compat.input('\n' + delimiter)
  500. except KeyboardInterrupt:
  501. exit(1)
  502. if __name__ == '__main__':
  503. import argparse
  504. parser = argparse.ArgumentParser(description='Run python demos')
  505. parser.add_argument('--noclear', '-C', action='store_true',
  506. help='Do not clear terminal on each slide')
  507. parser.add_argument('--rst', '-r', action='store_true',
  508. help='Highlight comments and dostrings as rst')
  509. parser.add_argument('--formatter', '-f', default='terminal',
  510. help='pygments formatter name could be: terminal, '
  511. 'terminal256, terminal16m')
  512. parser.add_argument('--style', '-s', default='default',
  513. help='pygments style name')
  514. parser.add_argument('--auto', '-a', action='store_true',
  515. help='Run all blocks automatically without'
  516. 'confirmation')
  517. parser.add_argument('--delimiter', '-d', default='...',
  518. help='slides delimiter added after each slide run')
  519. parser.add_argument('file', nargs=1,
  520. help='python demo file')
  521. args = parser.parse_args()
  522. slide(args.file[0], noclear=args.noclear, format_rst=args.rst,
  523. formatter=args.formatter, style=args.style, auto_all=args.auto,
  524. delimiter=args.delimiter)