Show More
@@ -0,0 +1,68 b'' | |||
|
1 | from IPython.demo import Demo,IPythonDemo,LineDemo,IPythonLineDemo,ClearDemo,ClearIPDemo | |
|
2 | import tempfile, os, StringIO, shutil | |
|
3 | ||
|
4 | example1 = """ | |
|
5 | '''A simple interactive demo to illustrate the use of IPython's Demo class.''' | |
|
6 | ||
|
7 | print 'Hello, welcome to an interactive IPython demo.' | |
|
8 | ||
|
9 | # The mark below defines a block boundary, which is a point where IPython will | |
|
10 | # stop execution and return to the interactive prompt. The dashes are actually | |
|
11 | # optional and used only as a visual aid to clearly separate blocks while | |
|
12 | # editing the demo code. | |
|
13 | # <demo> stop | |
|
14 | ||
|
15 | x = 1 | |
|
16 | y = 2 | |
|
17 | ||
|
18 | # <demo> stop | |
|
19 | ||
|
20 | # the mark below makes this block as silent | |
|
21 | # <demo> silent | |
|
22 | ||
|
23 | print 'This is a silent block, which gets executed but not printed.' | |
|
24 | ||
|
25 | # <demo> stop | |
|
26 | # <demo> auto | |
|
27 | print 'This is an automatic block.' | |
|
28 | print 'It is executed without asking for confirmation, but printed.' | |
|
29 | z = x+y | |
|
30 | ||
|
31 | print 'z=',x | |
|
32 | ||
|
33 | # <demo> stop | |
|
34 | # This is just another normal block. | |
|
35 | print 'z is now:', z | |
|
36 | ||
|
37 | print 'bye!' | |
|
38 | """ | |
|
39 | fp = tempfile.mkdtemp(prefix = 'DemoTmp') | |
|
40 | fd, filename = tempfile.mkstemp(prefix = 'demoExample1File', suffix = '.py', dir = fp) | |
|
41 | f = os.fdopen(fd, 'wt') | |
|
42 | ||
|
43 | f.write(example1) | |
|
44 | f.close() | |
|
45 | ||
|
46 | my_d = Demo(filename) | |
|
47 | my_cd = ClearDemo(filename) | |
|
48 | ||
|
49 | fobj = StringIO.StringIO(example1) | |
|
50 | str_d = Demo(fobj, title='via stringio') | |
|
51 | #~ def tmpcleanup(): | |
|
52 | #~ global my_d, my_cd, fp | |
|
53 | #~ del my_d | |
|
54 | #~ del my_cd | |
|
55 | #~ shutil.rmtree(fp, False) | |
|
56 | ||
|
57 | print ''' | |
|
58 | The example that is embeded in demo.py file has been used to create | |
|
59 | the following 3 demos, and should now be available to use: | |
|
60 | my_d() -- created from a file | |
|
61 | my_cd() -- created from a file, a ClearDemo | |
|
62 | str_d() -- same as above, but created via a stringi\o object | |
|
63 | Call by typing their name, (with parentheses), at the | |
|
64 | ipython prompt, interact with the block, then call again | |
|
65 | to run the next block. | |
|
66 | ''' | |
|
67 | # call tmpcleanup to delete the temporary files created. -not implemented | |
|
68 |
@@ -111,7 +111,11 b' has a few useful methods for navigation, like again(), edit(), jump(), seek()' | |||
|
111 | 111 | and back(). It can be reset for a new run via reset() or reloaded from disk |
|
112 | 112 | (in case you've edited the source) via reload(). See their docstrings below. |
|
113 | 113 | |
|
114 | ||
|
114 | Note: To make this simpler to explore, a file called "demoExercizer.py" has | |
|
115 | been added to the \ipython\docs\examples\core. Just cd to this directory in | |
|
116 | an IPython session, and type: | |
|
117 | run demoExercizer.py | |
|
118 | and then follow the directions. | |
|
115 | 119 | Example |
|
116 | 120 | ======= |
|
117 | 121 | |
@@ -125,7 +129,7 b" print 'Hello, welcome to an interactive IPython demo.'" | |||
|
125 | 129 | # The mark below defines a block boundary, which is a point where IPython will |
|
126 | 130 | # stop execution and return to the interactive prompt. The dashes are actually |
|
127 | 131 | # optional and used only as a visual aid to clearly separate blocks while |
|
128 | editing the demo code. | |
|
132 | # editing the demo code. | |
|
129 | 133 | # <demo> stop |
|
130 | 134 | |
|
131 | 135 | x = 1 |
@@ -170,6 +174,7 b' import sys' | |||
|
170 | 174 | |
|
171 | 175 | from IPython.PyColorize import Parser |
|
172 | 176 | from IPython.genutils import marquee, file_read, file_readlines |
|
177 | from genutils import Term | |
|
173 | 178 | |
|
174 | 179 | __all__ = ['Demo','IPythonDemo','LineDemo','IPythonLineDemo','DemoError'] |
|
175 | 180 | |
@@ -185,7 +190,7 b' class Demo(object):' | |||
|
185 | 190 | re_auto = re_mark('auto') |
|
186 | 191 | re_auto_all = re_mark('auto_all') |
|
187 | 192 | |
|
188 |
def __init__(self, |
|
|
193 | def __init__(self,src,title='',arg_str='',auto_all=None): | |
|
189 | 194 | """Make a new demo object. To run the demo, simply call the object. |
|
190 | 195 | |
|
191 | 196 | See the module docstring for full details and an example (you can use |
@@ -193,10 +198,15 b' class Demo(object):' | |||
|
193 | 198 | |
|
194 | 199 | Inputs: |
|
195 | 200 | |
|
196 | - fname = filename. | |
|
201 | - src is either a file, or file-like object, or a | |
|
202 | string that can be resolved to a filename. | |
|
197 | 203 | |
|
198 | 204 | Optional inputs: |
|
199 | 205 | |
|
206 | - title: a string to use as the demo name. Of most use when the demo | |
|
207 | you are making comes from an object that has no filename, or if you | |
|
208 | want an alternate denotation distinct from the filename. | |
|
209 | ||
|
200 | 210 | - arg_str(''): a string of arguments, internally converted to a list |
|
201 | 211 | just like sys.argv, so the demo script can see a similar |
|
202 | 212 | environment. |
@@ -207,9 +217,24 b' class Demo(object):' | |||
|
207 | 217 | can be changed at runtime simply by reassigning it to a boolean |
|
208 | 218 | value. |
|
209 | 219 | """ |
|
210 | ||
|
211 | self.fname = fname | |
|
212 | self.sys_argv = [fname] + shlex.split(arg_str) | |
|
220 | if hasattr(src, "read"): | |
|
221 | # It seems to be a file or a file-like object | |
|
222 | self.fobj = src | |
|
223 | self.fname = "from a file-like object" | |
|
224 | if title == '': | |
|
225 | self.title = "from a file-like object" | |
|
226 | else: | |
|
227 | self.title = title | |
|
228 | else: | |
|
229 | # Assume it's a string or something that can be converted to one | |
|
230 | self.fobj = open(src) | |
|
231 | self.fname = src | |
|
232 | if title == '': | |
|
233 | (filepath, filename) = os.path.split(src) | |
|
234 | self.title = filename | |
|
235 | else: | |
|
236 | self.title = title | |
|
237 | self.sys_argv = [src] + shlex.split(arg_str) | |
|
213 | 238 | self.auto_all = auto_all |
|
214 | 239 | |
|
215 | 240 | # get a few things from ipython. While it's a bit ugly design-wise, |
@@ -228,7 +253,7 b' class Demo(object):' | |||
|
228 | 253 | def reload(self): |
|
229 | 254 | """Reload source from disk and initialize state.""" |
|
230 | 255 | # read data and parse into blocks |
|
231 |
self.src = |
|
|
256 | self.src = self.fobj.read() | |
|
232 | 257 | src_b = [b.strip() for b in self.re_stop.split(self.src) if b] |
|
233 | 258 | self._silent = [bool(self.re_silent.findall(b)) for b in src_b] |
|
234 | 259 | self._auto = [bool(self.re_auto.findall(b)) for b in src_b] |
@@ -277,7 +302,7 b' class Demo(object):' | |||
|
277 | 302 | |
|
278 | 303 | if index is None: |
|
279 | 304 | if self.finished: |
|
280 | print 'Demo finished. Use reset() if you want to rerun it.' | |
|
305 | print >>Term.cout, 'Demo finished. Use <demo_name>.reset() if you want to rerun it.' | |
|
281 | 306 | return None |
|
282 | 307 | index = self.block_index |
|
283 | 308 | else: |
@@ -346,26 +371,27 b' class Demo(object):' | |||
|
346 | 371 | if index is None: |
|
347 | 372 | return |
|
348 | 373 | |
|
349 | print self.marquee('<%s> block # %s (%s remaining)' % | |
|
350 |
(self. |
|
|
351 |
|
|
|
374 | print >>Term.cout, self.marquee('<%s> block # %s (%s remaining)' % | |
|
375 | (self.title,index,self.nblocks-index-1)) | |
|
376 | print >>Term.cout,(self.src_blocks_colored[index]) | |
|
352 | 377 | sys.stdout.flush() |
|
353 | 378 | |
|
354 | 379 | def show_all(self): |
|
355 | 380 | """Show entire demo on screen, block by block""" |
|
356 | 381 | |
|
357 |
fname = self. |
|
|
382 | fname = self.title | |
|
383 | title = self.title | |
|
358 | 384 | nblocks = self.nblocks |
|
359 | 385 | silent = self._silent |
|
360 | 386 | marquee = self.marquee |
|
361 | 387 | for index,block in enumerate(self.src_blocks_colored): |
|
362 | 388 | if silent[index]: |
|
363 | print marquee('<%s> SILENT block # %s (%s remaining)' % | |
|
364 |
( |
|
|
389 | print >>Term.cout, marquee('<%s> SILENT block # %s (%s remaining)' % | |
|
390 | (title,index,nblocks-index-1)) | |
|
365 | 391 | else: |
|
366 | print marquee('<%s> block # %s (%s remaining)' % | |
|
367 |
( |
|
|
368 | print block, | |
|
392 | print >>Term.cout, marquee('<%s> block # %s (%s remaining)' % | |
|
393 | (title,index,nblocks-index-1)) | |
|
394 | print >>Term.cout, block, | |
|
369 | 395 | sys.stdout.flush() |
|
370 | 396 | |
|
371 | 397 | def runlines(self,source): |
@@ -390,18 +416,18 b' class Demo(object):' | |||
|
390 | 416 | next_block = self.src_blocks[index] |
|
391 | 417 | self.block_index += 1 |
|
392 | 418 | if self._silent[index]: |
|
393 | print marquee('Executing silent block # %s (%s remaining)' % | |
|
419 | print >>Term.cout, marquee('Executing silent block # %s (%s remaining)' % | |
|
394 | 420 | (index,self.nblocks-index-1)) |
|
395 | 421 | else: |
|
396 | 422 | self.pre_cmd() |
|
397 | 423 | self.show(index) |
|
398 | 424 | if self.auto_all or self._auto[index]: |
|
399 | print marquee('output:') | |
|
425 | print >>Term.cout, marquee('output:') | |
|
400 | 426 | else: |
|
401 | print marquee('Press <q> to quit, <Enter> to execute...'), | |
|
427 | print >>Term.cout, marquee('Press <q> to quit, <Enter> to execute...'), | |
|
402 | 428 | ans = raw_input().strip() |
|
403 | 429 | if ans: |
|
404 | print marquee('Block NOT executed') | |
|
430 | print >>Term.cout, marquee('Block NOT executed') | |
|
405 | 431 | return |
|
406 | 432 | try: |
|
407 | 433 | save_argv = sys.argv |
@@ -419,10 +445,10 b' class Demo(object):' | |||
|
419 | 445 | if self.block_index == self.nblocks: |
|
420 | 446 | mq1 = self.marquee('END OF DEMO') |
|
421 | 447 | if mq1: |
|
422 | # avoid spurious prints if empty marquees are used | |
|
423 | ||
|
424 | print mq1 | |
|
425 | print self.marquee('Use reset() if you want to rerun it.') | |
|
448 | # avoid spurious print >>Term.cout,s if empty marquees are used | |
|
449 | print >>Term.cout | |
|
450 | print >>Term.cout, mq1 | |
|
451 | print >>Term.cout, self.marquee('Use <demo_name>.reset() if you want to rerun it.') | |
|
426 | 452 | self.finished = True |
|
427 | 453 | |
|
428 | 454 | # These methods are meant to be overridden by subclasses who may wish to |
@@ -471,9 +497,9 b' class LineDemo(Demo):' | |||
|
471 | 497 | def reload(self): |
|
472 | 498 | """Reload source from disk and initialize state.""" |
|
473 | 499 | # read data and parse into blocks |
|
474 |
src_b = [l for l in |
|
|
500 | src_b = [l for l in self.fobj.readline() if l.strip()] | |
|
475 | 501 | nblocks = len(src_b) |
|
476 |
self.src = os.linesep.join( |
|
|
502 | self.src = os.linesep.join(self.fobj.readlines()) | |
|
477 | 503 | self._silent = [False]*nblocks |
|
478 | 504 | self._auto = [True]*nblocks |
|
479 | 505 | self.auto_all = True |
@@ -515,8 +541,8 b' class ClearMixin(object):' | |||
|
515 | 541 | """Method called before executing each block. |
|
516 | 542 | |
|
517 | 543 | This one simply clears the screen.""" |
|
518 | os.system('clear') | |
|
519 | ||
|
544 | import IPython.platutils | |
|
545 | IPython.platutils.term_clear() | |
|
520 | 546 | |
|
521 | 547 | class ClearDemo(ClearMixin,Demo): |
|
522 | 548 | pass |
@@ -35,6 +35,9 b' else:' | |||
|
35 | 35 | # there is a public, cross-platform way of toggling the term title control on |
|
36 | 36 | # and off. We should make this a stateful object later on so that each user |
|
37 | 37 | # can have its own instance if needed. |
|
38 | def term_clear(): | |
|
39 | _platutils.term_clear() | |
|
40 | ||
|
38 | 41 | def toggle_set_term_title(val): |
|
39 | 42 | """Control whether set_term_title is active or not. |
|
40 | 43 | |
@@ -65,6 +68,8 b' def set_term_title(title):' | |||
|
65 | 68 | # Deprecated functions |
|
66 | 69 | #----------------------------------------------------------------------------- |
|
67 | 70 | def freeze_term_title(): |
|
71 | import warnings | |
|
68 | 72 | warnings.warn("This function is deprecated, use toggle_set_term_title()") |
|
69 | 73 | _platutils.ignore_termtitle = True |
|
74 | del warnings | |
|
70 | 75 |
General Comments 0
You need to be logged in to leave comments.
Login now