# HG changeset patch
# User Mitesh Patel <qed777@gmail.com>
# Date 1260713762 28800
# Node ID 74de54884f5d240d5aac584b3f8555f52ad3ab10
# Parent 90ef66e9a9c9d5e35cf81ff7768c5cb4cebae918
#7650/sagenb: Fix doctesting under sagenb/
diff --git a/sagenb/misc/ipaddr.py b/sagenb/misc/ipaddr.py
a
|
b
|
|
| 1 | """nodoctests""" |
1 | 2 | #!/usr/bin/python |
2 | 3 | # |
3 | 4 | # Copyright 2007 Google Inc. |
diff --git a/sagenb/notebook/all.py b/sagenb/notebook/all.py
a
|
b
|
from sage_email import email |
15 | 15 | from notebook_object import notebook, inotebook |
16 | 16 | |
17 | 17 | from interact import interact, input_box, slider, range_slider, selector, checkbox, input_grid, text_control, color_selector |
| 18 | |
| 19 | # For doctesting. |
| 20 | import sagenb |
diff --git a/sagenb/notebook/avatars.py b/sagenb/notebook/avatars.py
a
|
b
|
def user_type(avatarId): |
44 | 44 | Traceback (most recent call last): |
45 | 45 | ... |
46 | 46 | AttributeError: 'NoneType' object has no attribute 'user_is_admin' |
47 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 47 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
48 | 48 | sage: nb.create_default_users('password') |
49 | 49 | Creating default users. |
50 | 50 | sage: sagenb.notebook.twist.notebook = nb |
diff --git a/sagenb/notebook/cell.py b/sagenb/notebook/cell.py
a
|
b
|
class Cell_generic: |
53 | 53 | """ |
54 | 54 | Returns True if this cell contains the use of interact either as a |
55 | 55 | function call or a decorator. |
56 | | |
| 56 | |
57 | 57 | EXAMPLES:: |
58 | | |
| 58 | |
59 | 59 | sage: from sagenb.notebook.cell import Cell_generic |
60 | 60 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
61 | 61 | sage: Cell_generic.is_interactive_cell(C) |
… |
… |
class Cell_generic: |
67 | 67 | """ |
68 | 68 | Delete all output in this cell. This is not executed - it is an |
69 | 69 | abstract function that must be overwritten in a derived class. |
70 | | |
| 70 | |
71 | 71 | EXAMPLES: This function just raises a NotImplementedError, since it |
72 | | most be defined in derived class. |
73 | | |
74 | | :: |
75 | | |
| 72 | most be defined in derived class.:: |
| 73 | |
76 | 74 | sage: C = sagenb.notebook.cell.Cell_generic() |
77 | 75 | sage: C.delete_output() |
78 | 76 | Traceback (most recent call last): |
… |
… |
class Cell_generic: |
84 | 82 | def html_new_cell_before(self): |
85 | 83 | """ |
86 | 84 | Returns the HTML code for inserting a new cell before self. |
87 | | |
| 85 | |
88 | 86 | EXAMPLES:: |
89 | | |
| 87 | |
90 | 88 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
91 | 89 | sage: print C.html_new_cell_before() |
92 | 90 | <div class="insert_new_cell" id="insert_new_cell_before0">... |
… |
… |
class Cell_generic: |
101 | 99 | def html_new_cell_after(self): |
102 | 100 | """ |
103 | 101 | Returns the HTML code for inserting a new cell after self. |
104 | | |
| 102 | |
105 | 103 | EXAMPLES:: |
106 | | |
| 104 | |
107 | 105 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
108 | 106 | sage: print C.html_new_cell_after() |
109 | 107 | <div class="insert_new_cell" id="insert_new_cell_after0">... |
… |
… |
class TextCell(Cell_generic): |
120 | 118 | def __init__(self, id, text, worksheet): |
121 | 119 | """ |
122 | 120 | EXAMPLES:: |
123 | | |
| 121 | |
124 | 122 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
125 | 123 | sage: C == loads(dumps(C)) |
126 | 124 | True |
… |
… |
class TextCell(Cell_generic): |
132 | 130 | def __repr__(self): |
133 | 131 | """ |
134 | 132 | String representation of this text cell. |
135 | | |
| 133 | |
136 | 134 | EXAMPLES:: |
137 | | |
| 135 | |
138 | 136 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
139 | 137 | sage: C.__repr__() |
140 | 138 | 'TextCell 0: 2+3' |
… |
… |
class TextCell(Cell_generic): |
145 | 143 | """ |
146 | 144 | Delete all output in this cell. This does nothing since text cells |
147 | 145 | have no output. |
148 | | |
| 146 | |
149 | 147 | EXAMPLES:: |
150 | | |
| 148 | |
151 | 149 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
152 | 150 | sage: C |
153 | 151 | TextCell 0: 2+3 |
… |
… |
class TextCell(Cell_generic): |
160 | 158 | def set_input_text(self, input_text): |
161 | 159 | """ |
162 | 160 | Sets the input text of self to be input_text. |
163 | | |
| 161 | |
164 | 162 | EXAMPLES:: |
165 | | |
| 163 | |
166 | 164 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
167 | 165 | sage: C |
168 | 166 | TextCell 0: 2+3 |
… |
… |
class TextCell(Cell_generic): |
171 | 169 | TextCell 0: 3+2 |
172 | 170 | """ |
173 | 171 | self.__text = input_text |
174 | | |
| 172 | |
175 | 173 | def set_worksheet(self, worksheet, id=None): |
176 | 174 | """ |
177 | 175 | Sets the worksheet object of self to be worksheet and optionally |
178 | 176 | changes the id of self. |
179 | | |
| 177 | |
180 | 178 | EXAMPLES:: |
181 | | |
| 179 | |
182 | 180 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
183 | 181 | sage: W = "worksheet object" |
184 | 182 | sage: C.set_worksheet(W) |
… |
… |
class TextCell(Cell_generic): |
195 | 193 | def worksheet(self): |
196 | 194 | """ |
197 | 195 | Returns the worksheet object associated to self. |
198 | | |
| 196 | |
199 | 197 | EXAMPLES:: |
200 | | |
| 198 | |
201 | 199 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', 'worksheet object') |
202 | 200 | sage: C.worksheet() |
203 | 201 | 'worksheet object' |
… |
… |
class TextCell(Cell_generic): |
209 | 207 | Returns an HTML version of self as a string. |
210 | 208 | |
211 | 209 | INPUT: |
212 | | |
| 210 | |
213 | 211 | - ``do_math_parse`` - bool (default: True) |
214 | 212 | If True, call math_parse (defined in cell.py) |
215 | | on the html. |
| 213 | on the html. |
216 | 214 | |
217 | 215 | EXAMPLES:: |
218 | | |
| 216 | |
219 | 217 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
220 | 218 | sage: C.html() |
221 | | '<div class="text_cell" id="cell_text_0">2+3...' |
| 219 | '...<div class="text_cell" id="cell_text_0">2+3...' |
222 | 220 | sage: C.set_input_text("$2+3$") |
223 | 221 | sage: C.html(do_math_parse=True) |
224 | | '<div class="text_cell" id="cell_text_0"><span class="math">2+3</span>...' |
| 222 | '...<div class="text_cell" id="cell_text_0"><span class="math">2+3</span>...' |
225 | 223 | """ |
226 | 224 | s = '<span id="cell_outer_%s">'%self.__id |
227 | 225 | |
… |
… |
class TextCell(Cell_generic): |
229 | 227 | s += self.html_new_cell_before() |
230 | 228 | |
231 | 229 | s += """<div class="text_cell" id="cell_text_%s">%s</div>"""%( |
232 | | self.__id, |
| 230 | self.__id, |
233 | 231 | self.html_inner(ncols=ncols, do_print=do_print, do_math_parse=do_math_parse, editing=editing)) |
234 | 232 | |
235 | 233 | if JEDITABLE_TINYMCE and hasattr(self.worksheet(),'is_published') and not self.worksheet().is_published() and not self.worksheet().docbrowser() and not do_print: |
… |
… |
class TextCell(Cell_generic): |
240 | 238 | print msg |
241 | 239 | # better to get the worksheet at all than to get a blank screen and nothing. |
242 | 240 | z = self.__text |
243 | | |
| 241 | |
244 | 242 | s += """<script>$("#cell_text_%s").unbind('dblclick').editable(function(value,settings) { |
245 | 243 | evaluate_text_cell_input(%s,value,settings); |
246 | 244 | return(value); |
247 | | }, { |
| 245 | }, { |
248 | 246 | tooltip : "", |
249 | 247 | placeholder : "", |
250 | 248 | // type : 'textarea', |
… |
… |
return(value); |
271 | 269 | Returns an HTML version of the content of self as a string. |
272 | 270 | |
273 | 271 | INPUT: |
274 | | |
| 272 | |
275 | 273 | - ``do_math_parse`` - bool (default: True) |
276 | 274 | If True, call math_parse (defined in cell.py) |
277 | | on the html. |
| 275 | on the html. |
278 | 276 | |
279 | 277 | EXAMPLES:: |
280 | | |
| 278 | |
281 | 279 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
282 | 280 | sage: C.html_inner() |
283 | 281 | '2+3...' |
… |
… |
return(value); |
297 | 295 | pass |
298 | 296 | s = """%s"""%t |
299 | 297 | return s |
300 | | |
| 298 | |
301 | 299 | |
302 | 300 | def plain_text(self, prompts=False): |
303 | 301 | """ |
304 | 302 | Returns a plain text version of self. |
305 | | |
| 303 | |
306 | 304 | EXAMPLES:: |
307 | | |
| 305 | |
308 | 306 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
309 | 307 | sage: C.plain_text() |
310 | 308 | '2+3' |
… |
… |
return(value); |
314 | 312 | def edit_text(self): |
315 | 313 | """ |
316 | 314 | Returns the text to be displayed in the Edit window. |
317 | | |
| 315 | |
318 | 316 | EXAMPLES:: |
319 | | |
| 317 | |
320 | 318 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
321 | 319 | sage: C.edit_text() |
322 | 320 | '2+3' |
… |
… |
return(value); |
330 | 328 | OUTPUT: |
331 | 329 | |
332 | 330 | - int -- self's ID. |
333 | | |
| 331 | |
334 | 332 | EXAMPLES:: |
335 | | |
| 333 | |
336 | 334 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
337 | 335 | sage: C.id() |
338 | 336 | 0 |
… |
… |
return(value); |
342 | 340 | def is_auto_cell(self): |
343 | 341 | """ |
344 | 342 | Returns True if self is automatically evaluated. |
345 | | |
| 343 | |
346 | 344 | EXAMPLES:: |
347 | | |
| 345 | |
348 | 346 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
349 | 347 | sage: C.is_auto_cell() |
350 | 348 | False |
… |
… |
return(value); |
354 | 352 | def __cmp__(self, right): |
355 | 353 | """ |
356 | 354 | Compares cells by ID. |
357 | | |
| 355 | |
358 | 356 | EXAMPLES:: |
359 | | |
| 357 | |
360 | 358 | sage: C1 = sagenb.notebook.cell.TextCell(0, '2+3', None) |
361 | 359 | sage: C2 = sagenb.notebook.cell.TextCell(0, '3+2', None) |
362 | 360 | sage: C3 = sagenb.notebook.cell.TextCell(1, '2+3', None) |
… |
… |
return(value); |
372 | 370 | def set_cell_output_type(self, typ='wrap'): |
373 | 371 | """ |
374 | 372 | This does nothing for TextCells. |
375 | | |
| 373 | |
376 | 374 | EXAMPLES:: |
377 | | |
| 375 | |
378 | 376 | sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None) |
379 | 377 | sage: C.set_cell_output_type("wrap") |
380 | 378 | """ |
381 | 379 | pass # ignored |
382 | | |
| 380 | |
383 | 381 | |
384 | 382 | class Cell(Cell_generic): |
385 | 383 | def __init__(self, id, input, out, worksheet): |
386 | 384 | """ |
387 | 385 | EXAMPLES:: |
388 | | |
| 386 | |
389 | 387 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
390 | 388 | sage: C == loads(dumps(C)) |
391 | 389 | True |
… |
… |
class Cell(Cell_generic): |
400 | 398 | self.__asap = False |
401 | 399 | self.__version = -1 |
402 | 400 | self.set_input_text(str(input).replace('\r','')) |
403 | | |
| 401 | |
404 | 402 | def set_asap(self, asap): |
405 | 403 | """ |
406 | 404 | Set whether this cell is evaluated as soon as possible. |
407 | | |
| 405 | |
408 | 406 | EXAMPLES:: |
409 | | |
| 407 | |
410 | 408 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
411 | 409 | sage: C.is_asap() |
412 | 410 | False |
… |
… |
class Cell(Cell_generic): |
420 | 418 | """ |
421 | 419 | Return True if this is an asap cell, i.e., evaluation of it is done |
422 | 420 | as soon as possible. |
423 | | |
| 421 | |
424 | 422 | EXAMPLES:: |
425 | | |
| 423 | |
426 | 424 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
427 | 425 | sage: C.is_asap() |
428 | 426 | False |
… |
… |
class Cell(Cell_generic): |
439 | 437 | def delete_output(self): |
440 | 438 | """ |
441 | 439 | Delete all output in this cell. |
442 | | |
| 440 | |
443 | 441 | EXAMPLES:: |
444 | | |
| 442 | |
445 | 443 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None); C |
446 | 444 | Cell 0; in=2+3, out=5 |
447 | 445 | sage: C.delete_output() |
… |
… |
class Cell(Cell_generic): |
456 | 454 | r""" |
457 | 455 | Return True if this cell has been successfully evaluated in a |
458 | 456 | currently running session. |
459 | | |
| 457 | |
460 | 458 | This is not about whether the output of the cell is valid given the |
461 | 459 | input. |
462 | | |
| 460 | |
463 | 461 | OUTPUT: |
464 | | |
465 | | |
| 462 | |
466 | 463 | - ``bool`` - whether or not this cell has been |
467 | 464 | evaluated in this session |
468 | | |
469 | | |
| 465 | |
470 | 466 | EXAMPLES: We create a worksheet with a cell that has wrong output:: |
471 | | |
472 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 467 | |
| 468 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
473 | 469 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
474 | 470 | sage: W = nb.create_new_worksheet('Test', 'sage') |
475 | | sage: W.edit_save('Sage\n{{{\n2+3\n///\n20\n}}}') |
| 471 | sage: W.edit_save('{{{\n2+3\n///\n20\n}}}') |
476 | 472 | sage: C = W.cell_list()[0] |
477 | 473 | sage: C |
478 | 474 | Cell 0; in=2+3, out= |
479 | 475 | 20 |
480 | | |
| 476 | |
481 | 477 | We re-evaluate that input cell:: |
482 | | |
| 478 | |
483 | 479 | sage: C.evaluate() |
484 | | sage: W.check_comp(wait=9999) |
485 | | ('d', Cell 0; in=2+3, out= |
486 | | 5 |
487 | | ) |
488 | | |
| 480 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
| 481 | ('w', Cell 0; in=2+3, out=) |
| 482 | |
489 | 483 | Now the output is right:: |
490 | | |
491 | | sage: C |
| 484 | |
| 485 | sage: C # random output -- depends on computer speed |
492 | 486 | Cell 0; in=2+3, out= |
493 | | 5 |
494 | | |
495 | | And the cell is considered to have been evaluated. |
496 | | |
| 487 | |
| 488 | And the cell is considered to have been evaluated.:: |
| 489 | |
| 490 | sage: C.evaluated() # random output -- depends on computer speed |
| 491 | True |
| 492 | |
497 | 493 | :: |
498 | | |
499 | | sage: C.evaluated() |
500 | | True |
501 | | |
502 | | :: |
503 | | |
504 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 494 | |
| 495 | sage: W.quit() |
| 496 | sage: nb.delete() |
505 | 497 | """ |
506 | 498 | # Cells are never considered evaluated in a new session. |
507 | 499 | if not self.worksheet().compute_process_has_been_started(): |
… |
… |
class Cell(Cell_generic): |
519 | 511 | return False |
520 | 512 | return self.__evaluated |
521 | 513 | except AttributeError: |
522 | | # Default assumption is that cell has not been evaluated. |
| 514 | # Default assumption is that cell has not been evaluated. |
523 | 515 | self.__evaluated = False |
524 | 516 | return False |
525 | 517 | |
… |
… |
class Cell(Cell_generic): |
527 | 519 | """ |
528 | 520 | Sets whether or not this is an no_output cell, i.e., a cell for |
529 | 521 | which we don't care at all about the output. |
530 | | |
| 522 | |
531 | 523 | EXAMPLES:: |
532 | | |
| 524 | |
533 | 525 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
534 | 526 | sage: C.is_no_output() |
535 | 527 | False |
… |
… |
class Cell(Cell_generic): |
543 | 535 | """ |
544 | 536 | Return True if this is an no_output cell, i.e., a cell for which |
545 | 537 | we don't care at all about the output. |
546 | | |
| 538 | |
547 | 539 | EXAMPLES:: |
548 | | |
| 540 | |
549 | 541 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
550 | 542 | sage: C.is_no_output() |
551 | 543 | False |
… |
… |
class Cell(Cell_generic): |
562 | 554 | def set_cell_output_type(self, typ='wrap'): |
563 | 555 | """ |
564 | 556 | Sets the cell output type. |
565 | | |
| 557 | |
566 | 558 | EXAMPLES:: |
567 | | |
| 559 | |
568 | 560 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
569 | 561 | sage: C.cell_output_type() |
570 | 562 | 'wrap' |
… |
… |
class Cell(Cell_generic): |
577 | 569 | def cell_output_type(self): |
578 | 570 | """ |
579 | 571 | Returns the cell output type. |
580 | | |
| 572 | |
581 | 573 | EXAMPLES:: |
582 | | |
| 574 | |
583 | 575 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
584 | 576 | sage: C.cell_output_type() |
585 | 577 | 'wrap' |
… |
… |
class Cell(Cell_generic): |
597 | 589 | """ |
598 | 590 | Sets the worksheet object of self to be worksheet and optionally |
599 | 591 | changes the id of self. |
600 | | |
| 592 | |
601 | 593 | EXAMPLES:: |
602 | | |
| 594 | |
603 | 595 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
604 | 596 | sage: W = "worksheet object" |
605 | 597 | sage: C.set_worksheet(W) |
… |
… |
class Cell(Cell_generic): |
616 | 608 | def worksheet(self): |
617 | 609 | """ |
618 | 610 | Returns the worksheet object associated to self. |
619 | | |
| 611 | |
620 | 612 | EXAMPLES:: |
621 | | |
| 613 | |
622 | 614 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', 'worksheet object') |
623 | 615 | sage: C.worksheet() |
624 | 616 | 'worksheet object' |
… |
… |
class Cell(Cell_generic): |
629 | 621 | """ |
630 | 622 | Update the list of files with html-style links or embeddings for |
631 | 623 | this cell. |
632 | | |
| 624 | |
633 | 625 | For interactive cells the html output section is always empty, |
634 | 626 | mainly because there is no good way to distinguish content (e.g., |
635 | 627 | images in the current directory) that goes into the interactive |
… |
… |
class Cell(Cell_generic): |
637 | 629 | |
638 | 630 | EXAMPLES:: |
639 | 631 | |
640 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 632 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
641 | 633 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
642 | 634 | sage: W = nb.create_new_worksheet('Test', 'sage') |
643 | 635 | sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W) |
644 | 636 | sage: C.evaluate() |
645 | | sage: W.check_comp(wait=9999) |
| 637 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
646 | 638 | ('d', Cell 0; in=plot(sin(x),0,5), out= |
647 | 639 | <html><font color='black'><img src='cell://sage0.png'></font></html> |
648 | 640 | <BLANKLINE> |
649 | 641 | ) |
650 | 642 | sage: C.update_html_output() |
651 | | sage: C.output_html() |
| 643 | sage: C.output_html() # random output -- depends on computer speed |
652 | 644 | '<img src="/home/sage/0/cells/0/sage0.png?...">' |
| 645 | sage: W.quit() |
| 646 | sage: nb.delete() |
653 | 647 | """ |
654 | 648 | if self.is_interactive_cell(): |
655 | 649 | self.__out_html = "" |
… |
… |
class Cell(Cell_generic): |
659 | 653 | def id(self): |
660 | 654 | """ |
661 | 655 | Returns the id of self. |
662 | | |
| 656 | |
663 | 657 | EXAMPLES:: |
664 | | |
| 658 | |
665 | 659 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
666 | 660 | sage: C.id() |
667 | 661 | 0 |
… |
… |
class Cell(Cell_generic): |
671 | 665 | def set_id(self, id): |
672 | 666 | """ |
673 | 667 | Sets the id of self to id. |
674 | | |
| 668 | |
675 | 669 | EXAMPLES:: |
676 | | |
| 670 | |
677 | 671 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
678 | 672 | sage: C.set_id(2) |
679 | 673 | sage: C.id() |
… |
… |
class Cell(Cell_generic): |
684 | 678 | def worksheet(self): |
685 | 679 | """ |
686 | 680 | Returns the workseet associated to self. |
687 | | |
| 681 | |
688 | 682 | EXAMPLES:: |
689 | | |
690 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 683 | |
| 684 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
691 | 685 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
692 | 686 | sage: W = nb.create_new_worksheet('Test', 'sage') |
693 | 687 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
694 | 688 | sage: C.worksheet() is W |
695 | 689 | True |
696 | | |
697 | | :: |
698 | | |
699 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 690 | sage: nb.delete() |
700 | 691 | """ |
701 | 692 | return self.__worksheet |
702 | 693 | |
703 | 694 | def worksheet_filename(self): |
704 | 695 | """ |
705 | 696 | Returns the filename of the worksheet associated to self. |
706 | | |
| 697 | |
707 | 698 | EXAMPLES:: |
708 | | |
709 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 699 | |
| 700 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
710 | 701 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
711 | 702 | sage: W = nb.create_new_worksheet('Test', 'sage') |
712 | 703 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
713 | 704 | sage: C.worksheet_filename() |
714 | 705 | 'sage/0' |
715 | | |
716 | | :: |
717 | | |
718 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 706 | sage: nb.delete() |
719 | 707 | """ |
720 | 708 | return self.__worksheet.filename() |
721 | 709 | |
… |
… |
class Cell(Cell_generic): |
723 | 711 | def notebook(self): |
724 | 712 | """ |
725 | 713 | Returns the notebook object associated to self. |
726 | | |
| 714 | |
727 | 715 | EXAMPLES:: |
728 | | |
729 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 716 | |
| 717 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
730 | 718 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
731 | 719 | sage: W = nb.create_new_worksheet('Test', 'sage') |
732 | 720 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
733 | 721 | sage: C.notebook() is nb |
734 | 722 | True |
735 | | |
736 | | :: |
737 | | |
738 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 723 | sage: nb.delete() |
739 | 724 | """ |
740 | 725 | return self.__worksheet.notebook() |
741 | 726 | |
… |
… |
class Cell(Cell_generic): |
743 | 728 | """ |
744 | 729 | Returns the directory associated to self. If the directory doesn't |
745 | 730 | already exist, then this method creates it. |
746 | | |
| 731 | |
747 | 732 | EXAMPLES:: |
748 | | |
749 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 733 | |
| 734 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
750 | 735 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
751 | 736 | sage: W = nb.create_new_worksheet('Test', 'sage') |
752 | 737 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
753 | 738 | sage: C.directory() |
754 | | '.../worksheets/sage/0/cells/0' |
755 | | |
756 | | :: |
757 | | |
758 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 739 | '.../home/sage/0/cells/0' |
| 740 | sage: nb.delete() |
759 | 741 | """ |
760 | 742 | dir = self._directory_name() |
761 | 743 | if not os.path.exists(dir): |
… |
… |
class Cell(Cell_generic): |
766 | 748 | def _directory_name(self): |
767 | 749 | """ |
768 | 750 | Returns a string of the directory associated to self. |
769 | | |
| 751 | |
770 | 752 | EXAMPLES:: |
771 | | |
772 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 753 | |
| 754 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
773 | 755 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
774 | 756 | sage: W = nb.create_new_worksheet('Test', 'sage') |
775 | 757 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
776 | 758 | sage: C._directory_name() |
777 | | '.../worksheets/sage/0/cells/0' |
778 | | |
779 | | :: |
780 | | |
781 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 759 | '.../home/sage/0/cells/0' |
| 760 | sage: nb.delete() |
782 | 761 | """ |
783 | 762 | return os.path.join(self.__worksheet.directory(), 'cells', str(self.id())) |
784 | 763 | |
… |
… |
class Cell(Cell_generic): |
786 | 765 | def __cmp__(self, right): |
787 | 766 | """ |
788 | 767 | Compares cells by their IDs. |
789 | | |
| 768 | |
790 | 769 | EXAMPLES:: |
791 | | |
| 770 | |
792 | 771 | sage: C1 = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
793 | 772 | sage: C2 = sagenb.notebook.cell.Cell(0, '3+2', '5', None) |
794 | 773 | sage: C3 = sagenb.notebook.cell.Cell(1, '2+3', '5', None) |
… |
… |
class Cell(Cell_generic): |
804 | 783 | def __repr__(self): |
805 | 784 | """ |
806 | 785 | Returns a string representation of self. |
807 | | |
| 786 | |
808 | 787 | EXAMPLES:: |
809 | | |
| 788 | |
810 | 789 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None); C |
811 | 790 | Cell 0; in=2+3, out=5 |
812 | 791 | """ |
… |
… |
class Cell(Cell_generic): |
816 | 795 | """ |
817 | 796 | Returns the number of columns for word wrapping. This defaults to |
818 | 797 | 70, but the default setting for a notebook is 72. |
819 | | |
| 798 | |
820 | 799 | EXAMPLES:: |
821 | | |
| 800 | |
822 | 801 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
823 | 802 | sage: C.word_wrap_cols() |
824 | 803 | 70 |
825 | | |
826 | | :: |
827 | | |
828 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 804 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
829 | 805 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
830 | 806 | sage: W = nb.create_new_worksheet('Test', 'sage') |
831 | 807 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
832 | 808 | sage: C.word_wrap_cols() |
833 | 809 | 72 |
834 | | |
835 | | :: |
836 | | |
837 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 810 | sage: nb.delete() |
838 | 811 | """ |
839 | 812 | try: |
840 | 813 | return self.notebook().conf()['word_wrap_cols'] |
841 | 814 | except AttributeError: |
842 | 815 | return 70 |
843 | | |
| 816 | |
844 | 817 | def plain_text(self, ncols=0, prompts=True, max_out=None): |
845 | 818 | r""" |
846 | 819 | Returns the plain text version of self. |
… |
… |
class Cell(Cell_generic): |
857 | 830 | |
858 | 831 | input_lines = self.__in |
859 | 832 | pr = 'sage: ' |
860 | | |
| 833 | |
861 | 834 | if prompts: |
862 | 835 | input_lines = input_lines.splitlines() |
863 | 836 | has_prompt = False |
… |
… |
class Cell(Cell_generic): |
911 | 884 | if not max_out is None and len(out) > max_out: |
912 | 885 | out = out[:max_out] + '...' |
913 | 886 | |
914 | | # Get rid of spurious carriage returns |
| 887 | # Get rid of spurious carriage returns |
915 | 888 | s = s.strip('\n') |
916 | 889 | out = out.strip('\n').strip('\r').strip('\r\n') |
917 | 890 | s = s + '\n' + out |
… |
… |
class Cell(Cell_generic): |
919 | 892 | if not prompts: |
920 | 893 | s = s.rstrip('\n') |
921 | 894 | return s |
922 | | |
| 895 | |
923 | 896 | def edit_text(self, ncols=0, prompts=False, max_out=None): |
924 | 897 | r""" |
925 | 898 | Returns the text displayed in the Edit window. |
926 | | |
| 899 | |
927 | 900 | EXAMPLES:: |
928 | | |
| 901 | |
929 | 902 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
930 | 903 | sage: C.edit_text() |
931 | 904 | '{{{id=0|\n2+3\n///\n5\n}}}' |
… |
… |
class Cell(Cell_generic): |
936 | 909 | def is_last(self): |
937 | 910 | """ |
938 | 911 | Returns True if self is the last cell in the worksheet. |
939 | | |
| 912 | |
940 | 913 | EXAMPLES:: |
941 | | |
942 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 914 | |
| 915 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
943 | 916 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
944 | 917 | sage: W = nb.create_new_worksheet('Test', 'sage') |
945 | 918 | sage: C = W.new_cell_after(0, "2^2"); C |
946 | | Cell 1; in=2^2, out= |
| 919 | Cell 2; in=2^2, out= |
947 | 920 | sage: C.is_last() |
948 | 921 | True |
949 | 922 | sage: C = W.get_cell_with_id(0) |
950 | 923 | sage: C.is_last() |
951 | 924 | False |
952 | | |
953 | | :: |
954 | | |
955 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 925 | sage: nb.delete() |
956 | 926 | """ |
957 | 927 | return self.__worksheet.cell_list()[-1] == self |
958 | 928 | |
… |
… |
class Cell(Cell_generic): |
961 | 931 | Returns the id of the next cell in the worksheet associated to |
962 | 932 | self. If self is not in the worksheet or self is the last cell in |
963 | 933 | the cell_list, then the id of the first cell is returned. |
964 | | |
| 934 | |
965 | 935 | EXAMPLES:: |
966 | | |
967 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 936 | |
| 937 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
968 | 938 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
969 | 939 | sage: W = nb.create_new_worksheet('Test', 'sage') |
970 | | sage: C = W.new_cell_after(0, "2^2") |
971 | | sage: C = W.get_cell_with_id(0) |
| 940 | sage: C = W.new_cell_after(1, "2^2") |
| 941 | sage: C = W.get_cell_with_id(1) |
| 942 | sage: C.next_id() |
| 943 | 2 |
| 944 | sage: C = W.get_cell_with_id(2) |
972 | 945 | sage: C.next_id() |
973 | 946 | 1 |
974 | | sage: C = W.get_cell_with_id(1) |
975 | | sage: C.next_id() |
976 | | 0 |
977 | | |
978 | | :: |
979 | | |
980 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 947 | sage: nb.delete() |
981 | 948 | """ |
982 | 949 | L = self.__worksheet.cell_list() |
983 | 950 | try: |
… |
… |
class Cell(Cell_generic): |
993 | 960 | def interrupt(self): |
994 | 961 | """ |
995 | 962 | Record that the calculation running in this cell was interrupted. |
996 | | |
| 963 | |
997 | 964 | EXAMPLES:: |
998 | | |
999 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 965 | |
| 966 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1000 | 967 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1001 | 968 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1002 | 969 | sage: C = W.new_cell_after(0, "2^2") |
… |
… |
class Cell(Cell_generic): |
1005 | 972 | True |
1006 | 973 | sage: C.evaluated() |
1007 | 974 | False |
1008 | | |
1009 | | :: |
1010 | | |
1011 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 975 | sage: nb.delete() |
1012 | 976 | """ |
1013 | 977 | self.__interrupted = True |
1014 | 978 | self.__evaluated = False |
… |
… |
class Cell(Cell_generic): |
1016 | 980 | def interrupted(self): |
1017 | 981 | """ |
1018 | 982 | Returns True if the evaluation of this cell has been interrupted. |
1019 | | |
| 983 | |
1020 | 984 | EXAMPLES:: |
1021 | | |
1022 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 985 | |
| 986 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1023 | 987 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1024 | 988 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1025 | 989 | sage: C = W.new_cell_after(0, "2^2") |
1026 | 990 | sage: C.interrupt() |
1027 | 991 | sage: C.interrupted() |
1028 | 992 | True |
1029 | | |
1030 | | :: |
1031 | | |
1032 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 993 | sage: nb.delete() |
1033 | 994 | """ |
1034 | 995 | return self.__interrupted |
1035 | 996 | |
1036 | 997 | def computing(self): |
1037 | 998 | """ |
1038 | 999 | Returns True if self is in its worksheet's queue. |
1039 | | |
| 1000 | |
1040 | 1001 | EXAMPLES:: |
1041 | | |
1042 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1002 | |
| 1003 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1043 | 1004 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1044 | 1005 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1045 | 1006 | sage: C = W.new_cell_after(0, "2^2") |
1046 | 1007 | sage: C.computing() |
1047 | 1008 | False |
1048 | | |
1049 | | :: |
1050 | | |
1051 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 1009 | sage: nb.delete() |
1052 | 1010 | """ |
1053 | 1011 | return self in self.__worksheet.queue() |
1054 | 1012 | |
… |
… |
class Cell(Cell_generic): |
1056 | 1014 | r""" |
1057 | 1015 | Return True if this cell contains the use of interact either as a |
1058 | 1016 | function call or a decorator. |
1059 | | |
| 1017 | |
1060 | 1018 | EXAMPLES:: |
1061 | | |
1062 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1019 | |
| 1020 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1063 | 1021 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1064 | 1022 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1065 | 1023 | sage: C = W.new_cell_after(0, "@interact\ndef f(a=slider(0,10,1,5):\n print a^2") |
… |
… |
class Cell(Cell_generic): |
1068 | 1026 | sage: C = W.new_cell_after(C.id(), "2+2") |
1069 | 1027 | sage: C.is_interactive_cell() |
1070 | 1028 | False |
1071 | | |
1072 | | :: |
1073 | | |
1074 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 1029 | sage: nb.delete() |
1075 | 1030 | """ |
1076 | 1031 | # Do *not* cache |
1077 | 1032 | s = strip_string_literals(self.input_text()) |
… |
… |
class Cell(Cell_generic): |
1082 | 1037 | def is_interacting(self): |
1083 | 1038 | r""" |
1084 | 1039 | Returns True if this cell is currently interacting with the user. |
1085 | | |
| 1040 | |
1086 | 1041 | EXAMPLES:: |
1087 | | |
1088 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1042 | |
| 1043 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1089 | 1044 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1090 | 1045 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1091 | 1046 | sage: C = W.new_cell_after(0, "@interact\ndef f(a=slider(0,10,1,5):\n print a^2") |
… |
… |
class Cell(Cell_generic): |
1098 | 1053 | """ |
1099 | 1054 | Stops interaction with user. |
1100 | 1055 | |
1101 | | TODO: Add doctests for :meth:`stop_interacting`. |
| 1056 | TODO: Add doctests for :meth:`stop_interacting`. |
1102 | 1057 | """ |
1103 | 1058 | if self.is_interacting(): |
1104 | 1059 | del self.interact |
… |
… |
class Cell(Cell_generic): |
1106 | 1061 | def set_input_text(self, input): |
1107 | 1062 | """ |
1108 | 1063 | Sets the input text of self to be the string input. |
1109 | | |
| 1064 | |
1110 | 1065 | TODO: Add doctests for the code dealing with interact. |
1111 | | |
| 1066 | |
1112 | 1067 | EXAMPLES:: |
1113 | | |
1114 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1068 | |
| 1069 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1115 | 1070 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1116 | 1071 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1117 | 1072 | sage: C = W.new_cell_after(0, "2^2") |
1118 | 1073 | sage: C.evaluate() |
1119 | | sage: W.check_comp(wait=9999) |
| 1074 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
1120 | 1075 | ('d', Cell 1; in=2^2, out= |
1121 | 1076 | 4 |
1122 | 1077 | ) |
1123 | 1078 | sage: C.version() |
1124 | 1079 | 0 |
1125 | | |
1126 | | :: |
1127 | | |
1128 | 1080 | sage: C.set_input_text('3+3') |
1129 | 1081 | sage: C.input_text() |
1130 | 1082 | '3+3' |
… |
… |
class Cell(Cell_generic): |
1132 | 1084 | False |
1133 | 1085 | sage: C.version() |
1134 | 1086 | 1 |
1135 | | |
1136 | | :: |
1137 | | |
1138 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 1087 | sage: W.quit() |
| 1088 | sage: nb.delete() |
1139 | 1089 | """ |
1140 | 1090 | # Stuff to deal with interact |
1141 | 1091 | if input.startswith('%__sage_interact__'): |
… |
… |
class Cell(Cell_generic): |
1149 | 1099 | except AttributeError: |
1150 | 1100 | pass |
1151 | 1101 | |
1152 | | # We have updated the input text so the cell can't have |
1153 | | # been evaluated. |
| 1102 | # We have updated the input text so the cell can't have |
| 1103 | # been evaluated. |
1154 | 1104 | self.__evaluated = False |
1155 | 1105 | self.__version = self.version() + 1 |
1156 | 1106 | self.__in = input |
… |
… |
class Cell(Cell_generic): |
1160 | 1110 | #Run get the input text with all of the percent |
1161 | 1111 | #directives parsed |
1162 | 1112 | self._cleaned_input = self.parse_percent_directives() |
1163 | | |
| 1113 | |
1164 | 1114 | def input_text(self): |
1165 | 1115 | """ |
1166 | 1116 | Returns self's input text. |
1167 | | |
| 1117 | |
1168 | 1118 | EXAMPLES:: |
1169 | | |
| 1119 | |
1170 | 1120 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1171 | 1121 | sage: C.input_text() |
1172 | 1122 | '2+3' |
… |
… |
class Cell(Cell_generic): |
1180 | 1130 | text is returned. |
1181 | 1131 | |
1182 | 1132 | EXAMPLES:: |
1183 | | |
| 1133 | |
1184 | 1134 | sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None) |
1185 | 1135 | sage: C.cleaned_input_text() |
1186 | 1136 | '2+3' |
1187 | | |
1188 | 1137 | """ |
1189 | 1138 | if self.is_interacting(): |
1190 | 1139 | return self.interact |
… |
… |
class Cell(Cell_generic): |
1197 | 1146 | with the percent directives at the top removed. As it's doing |
1198 | 1147 | this, it computes a list of all the directives and which |
1199 | 1148 | system (if any) the cell should be run under. |
1200 | | |
| 1149 | |
1201 | 1150 | EXAMPLES:: |
1202 | | |
| 1151 | |
1203 | 1152 | sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None) |
1204 | 1153 | sage: C.parse_percent_directives() |
1205 | 1154 | '2+3' |
1206 | 1155 | sage: C.percent_directives() |
1207 | 1156 | ['hide', 'maxima'] |
1208 | | |
1209 | 1157 | """ |
1210 | 1158 | self._system = None |
1211 | 1159 | text = self.input_text().splitlines() |
… |
… |
class Cell(Cell_generic): |
1225 | 1173 | pass |
1226 | 1174 | else: |
1227 | 1175 | self._system = line[1:] |
1228 | | |
| 1176 | |
1229 | 1177 | directives.append(line[1:]) |
1230 | | |
| 1178 | |
1231 | 1179 | self._percent_directives = directives |
1232 | 1180 | return "\n".join(text[i:]).strip() |
1233 | | |
| 1181 | |
1234 | 1182 | def percent_directives(self): |
1235 | 1183 | r""" |
1236 | 1184 | Returns a list of all the percent directives that appear |
1237 | 1185 | in this cell. |
1238 | 1186 | |
1239 | 1187 | EXAMPLES:: |
1240 | | |
| 1188 | |
1241 | 1189 | sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None) |
1242 | 1190 | sage: C.percent_directives() |
1243 | 1191 | ['hide', 'maxima'] |
1244 | | |
1245 | 1192 | """ |
1246 | 1193 | return self._percent_directives |
1247 | 1194 | |
… |
… |
class Cell(Cell_generic): |
1256 | 1203 | worksheet's default system. |
1257 | 1204 | |
1258 | 1205 | EXAMPLES:: |
1259 | | |
| 1206 | |
1260 | 1207 | sage: C = sagenb.notebook.cell.Cell(0, '%maxima\n2+3', '5', None) |
1261 | 1208 | sage: C.system() |
1262 | 1209 | 'maxima' |
… |
… |
class Cell(Cell_generic): |
1271 | 1218 | def is_auto_cell(self): |
1272 | 1219 | r""" |
1273 | 1220 | Returns True if self is an auto cell. |
1274 | | |
| 1221 | |
1275 | 1222 | An auto cell is a cell that is automatically evaluated when the |
1276 | 1223 | worksheet starts up. |
1277 | | |
| 1224 | |
1278 | 1225 | EXAMPLES:: |
1279 | | |
| 1226 | |
1280 | 1227 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1281 | 1228 | sage: C.is_auto_cell() |
1282 | 1229 | False |
… |
… |
class Cell(Cell_generic): |
1291 | 1238 | Returns the changed input text for the cell. If there was any |
1292 | 1239 | changed input text, then it is reset to " before this method |
1293 | 1240 | returns. |
1294 | | |
| 1241 | |
1295 | 1242 | EXAMPLES:: |
1296 | | |
| 1243 | |
1297 | 1244 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1298 | 1245 | sage: C.changed_input_text() |
1299 | 1246 | '' |
… |
… |
class Cell(Cell_generic): |
1318 | 1265 | """ |
1319 | 1266 | Note that this does not update the version of the cell. This is |
1320 | 1267 | typically used for things like tab completion. |
1321 | | |
| 1268 | |
1322 | 1269 | EXAMPLES:: |
1323 | | |
| 1270 | |
1324 | 1271 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1325 | 1272 | sage: C.set_changed_input_text('3+3') |
1326 | 1273 | sage: C.input_text() |
… |
… |
class Cell(Cell_generic): |
1336 | 1283 | Sets the output text for self. |
1337 | 1284 | |
1338 | 1285 | EXAMPLES:: |
1339 | | |
| 1286 | |
1340 | 1287 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1341 | 1288 | sage: len(C.plain_text()) |
1342 | 1289 | 11 |
… |
… |
class Cell(Cell_generic): |
1349 | 1296 | output = '' |
1350 | 1297 | |
1351 | 1298 | # In interacting mode, we just save the computed output |
1352 | | # (do not overwrite). |
| 1299 | # (do not overwrite). |
1353 | 1300 | if self.is_interacting(): |
1354 | 1301 | self._interact_output = (output, html) |
1355 | 1302 | return |
1356 | | |
| 1303 | |
1357 | 1304 | if hasattr(self, '_html_cache'): |
1358 | 1305 | del self._html_cache |
1359 | 1306 | |
… |
… |
class Cell(Cell_generic): |
1386 | 1333 | def sage(self): |
1387 | 1334 | """ |
1388 | 1335 | TODO: Figure out what exactly this does. |
1389 | | |
| 1336 | |
1390 | 1337 | EXAMPLES:: |
1391 | | |
| 1338 | |
1392 | 1339 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1393 | 1340 | sage: C.sage() is None |
1394 | 1341 | True |
… |
… |
class Cell(Cell_generic): |
1403 | 1350 | Returns the HTML for self's output. |
1404 | 1351 | |
1405 | 1352 | EXAMPLES:: |
1406 | | |
| 1353 | |
1407 | 1354 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1408 | 1355 | sage: C.output_html() |
1409 | 1356 | '' |
… |
… |
class Cell(Cell_generic): |
1416 | 1363 | except AttributeError: |
1417 | 1364 | self.__out_html = '' |
1418 | 1365 | return '' |
1419 | | |
| 1366 | |
1420 | 1367 | def process_cell_urls(self, urls): |
1421 | 1368 | """ |
1422 | 1369 | Processes URLs of the form ``'cell://.*?'`` by replacing the |
… |
… |
class Cell(Cell_generic): |
1428 | 1375 | - ``urls`` - a string |
1429 | 1376 | |
1430 | 1377 | EXAMPLES:: |
1431 | | |
1432 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1378 | |
| 1379 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1433 | 1380 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1434 | 1381 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1435 | 1382 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
… |
… |
class Cell(Cell_generic): |
1449 | 1396 | INPUT: |
1450 | 1397 | |
1451 | 1398 | - ``ncols`` -- maximum number of columns |
1452 | | |
| 1399 | |
1453 | 1400 | - ``html`` -- boolean stating whether to output HTML |
1454 | | |
| 1401 | |
1455 | 1402 | - ``raw`` -- boolean stating whether to output raw text |
1456 | 1403 | (takes precedence over HTML) |
1457 | 1404 | |
… |
… |
class Cell(Cell_generic): |
1459 | 1406 | |
1460 | 1407 | EXAMPLES:: |
1461 | 1408 | |
1462 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1409 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1463 | 1410 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1464 | 1411 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1465 | 1412 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
… |
… |
class Cell(Cell_generic): |
1501 | 1448 | return '<h2>Click to the left again to hide and once more to show the dynamic interactive window</h2>' |
1502 | 1449 | else: |
1503 | 1450 | s = self.__out |
1504 | | |
| 1451 | |
1505 | 1452 | if raw: |
1506 | 1453 | return s |
1507 | 1454 | |
… |
… |
class Cell(Cell_generic): |
1510 | 1457 | |
1511 | 1458 | if not is_interact and not self.is_html() and len(s.strip()) > 0: |
1512 | 1459 | s = '<pre class="shrunk">' + s.strip('\n') + '</pre>' |
1513 | | |
| 1460 | |
1514 | 1461 | return s.strip('\n') |
1515 | 1462 | |
1516 | 1463 | def parse_html(self, s, ncols): |
… |
… |
class Cell(Cell_generic): |
1518 | 1465 | Parse HTML for output. |
1519 | 1466 | |
1520 | 1467 | INPUT: |
1521 | | |
| 1468 | |
1522 | 1469 | - ``s`` -- the input string containing HTML |
1523 | | |
| 1470 | |
1524 | 1471 | - ``ncols`` -- maximum number of columns |
1525 | | |
| 1472 | |
1526 | 1473 | EXAMPLES:: |
1527 | | |
1528 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1474 | |
| 1475 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1529 | 1476 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1530 | 1477 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1531 | 1478 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
… |
… |
class Cell(Cell_generic): |
1569 | 1516 | j = t[i:].lower().find('</script>') |
1570 | 1517 | if j == -1: break |
1571 | 1518 | t = t[:i] + t[i+j+len('</script>'):] |
1572 | | |
| 1519 | |
1573 | 1520 | return t |
1574 | | |
| 1521 | |
1575 | 1522 | |
1576 | 1523 | def has_output(self): |
1577 | 1524 | """ |
1578 | 1525 | Returns True if there is output for this cell. |
1579 | | |
| 1526 | |
1580 | 1527 | EXAMPLES:: |
1581 | | |
| 1528 | |
1582 | 1529 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1583 | 1530 | sage: C.has_output() |
1584 | 1531 | True |
… |
… |
class Cell(Cell_generic): |
1592 | 1539 | r""" |
1593 | 1540 | Returns True if this is an HTML cell. An HTML cell whose system is |
1594 | 1541 | 'html' and is typically specified by ``%html``. |
1595 | | |
| 1542 | |
1596 | 1543 | EXAMPLES:: |
1597 | | |
| 1544 | |
1598 | 1545 | sage: C = sagenb.notebook.cell.Cell(0, "%html\nTest HTML", None, None) |
1599 | 1546 | sage: C.system() |
1600 | 1547 | 'html' |
… |
… |
class Cell(Cell_generic): |
1603 | 1550 | sage: C = sagenb.notebook.cell.Cell(0, "Test HTML", None, None) |
1604 | 1551 | sage: C.is_html() |
1605 | 1552 | False |
1606 | | |
1607 | 1553 | """ |
1608 | 1554 | try: |
1609 | 1555 | return self.__is_html |
… |
… |
class Cell(Cell_generic): |
1613 | 1559 | def set_is_html(self, v): |
1614 | 1560 | """ |
1615 | 1561 | Sets whether or not this cell is an HTML cell. |
1616 | | |
| 1562 | |
1617 | 1563 | This is called by check_for_system_switching in worksheet.py. |
1618 | | |
| 1564 | |
1619 | 1565 | EXAMPLES:: |
1620 | | |
| 1566 | |
1621 | 1567 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1622 | 1568 | sage: C.is_html() |
1623 | 1569 | False |
… |
… |
class Cell(Cell_generic): |
1630 | 1576 | ################# |
1631 | 1577 | # Introspection # |
1632 | 1578 | ################# |
1633 | | def set_introspect_html(self, html, completing=False, verbose=False, raw=False): |
| 1579 | def set_introspect_html(self, html, completing=False, raw=False): |
1634 | 1580 | """ |
1635 | | If ``verbose`` is True, print verbose output about notebook |
1636 | | introspection to the command-line. However, the argument |
1637 | | ``verbose`` is not easily accessible now -- if you need to |
1638 | | debug, you have to edit this file, changing its value to True, |
1639 | | and run 'sage -b'. |
1640 | | |
1641 | 1581 | EXAMPLES:: |
1642 | 1582 | |
1643 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1583 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1644 | 1584 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1645 | 1585 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1646 | 1586 | sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W) |
1647 | 1587 | sage: C.introspect() |
1648 | 1588 | False |
1649 | 1589 | sage: C.evaluate(username='sage') |
1650 | | sage: W.check_comp(9999) |
| 1590 | sage: W.check_comp(9999) # random output -- depends on computer speed |
1651 | 1591 | ('d', Cell 0; in=sage?, out=) |
1652 | 1592 | sage: C.set_introspect_html('foobar') |
1653 | 1593 | sage: C.introspect_html() |
1654 | | '<div class="docstring"><pre>foobar</pre></div>' |
| 1594 | 'foobar' |
1655 | 1595 | sage: C.set_introspect_html('`foobar`') |
1656 | 1596 | sage: C.introspect_html() |
1657 | | '<div class="docstring">...<span class="math">foobar</span>...</div>' |
| 1597 | '`foobar`' |
| 1598 | sage: W.quit() |
| 1599 | sage: nb.delete() |
1658 | 1600 | """ |
1659 | 1601 | self.__introspect_html = html |
1660 | 1602 | self.introspection_status = 'done' |
1661 | | |
| 1603 | |
1662 | 1604 | def get_introspection_status(self): |
1663 | 1605 | try: |
1664 | 1606 | return self.__introspection_status |
… |
… |
class Cell(Cell_generic): |
1669 | 1611 | self.__introspection_status = value |
1670 | 1612 | |
1671 | 1613 | introspection_status = property(get_introspection_status, set_introspection_status) |
1672 | | |
1673 | | |
| 1614 | |
| 1615 | |
1674 | 1616 | def introspect_html(self): |
1675 | 1617 | """ |
1676 | 1618 | Returns HTML for introspection. |
1677 | 1619 | |
1678 | 1620 | EXAMPLES:: |
1679 | | |
1680 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1621 | |
| 1622 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1681 | 1623 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1682 | 1624 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1683 | 1625 | sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W) |
1684 | 1626 | sage: C.introspect() |
1685 | 1627 | False |
1686 | 1628 | sage: C.evaluate(username='sage') |
1687 | | sage: W.check_comp(9999) |
| 1629 | sage: W.check_comp(9999) # random output -- depends on computer speed |
1688 | 1630 | ('d', Cell 0; in=sage?, out=) |
1689 | | sage: C.introspect_html() |
1690 | | u'<div class="docstring">...</pre></div>' |
| 1631 | sage: C.introspect_html() # random output -- depends on computer speed |
| 1632 | u'...<div class="docstring">...sage...</pre></div>...' |
| 1633 | sage: W.quit() |
| 1634 | sage: nb.delete() |
1691 | 1635 | """ |
1692 | 1636 | if not self.introspect(): |
1693 | 1637 | return '' |
… |
… |
class Cell(Cell_generic): |
1700 | 1644 | def introspect(self): |
1701 | 1645 | """ |
1702 | 1646 | Returns self's introspection text. |
1703 | | |
| 1647 | |
1704 | 1648 | EXAMPLES:: |
1705 | 1649 | |
1706 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1650 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1707 | 1651 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1708 | 1652 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1709 | 1653 | sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W) |
1710 | 1654 | sage: C.introspect() |
1711 | 1655 | False |
1712 | 1656 | sage: C.evaluate(username='sage') |
1713 | | sage: W.check_comp(9999) |
| 1657 | sage: W.check_comp(9999) # random output -- depends on computer speed |
1714 | 1658 | ('d', Cell 0; in=sage?, out=) |
1715 | 1659 | sage: C.introspect() |
1716 | 1660 | ['sage?', ''] |
| 1661 | sage: W.quit() |
| 1662 | sage: nb.delete() |
1717 | 1663 | """ |
1718 | 1664 | try: |
1719 | 1665 | return self.__introspect |
… |
… |
class Cell(Cell_generic): |
1723 | 1669 | def unset_introspect(self): |
1724 | 1670 | """ |
1725 | 1671 | Unsets self's introspection text. |
1726 | | |
| 1672 | |
1727 | 1673 | EXAMPLES:: |
1728 | 1674 | |
1729 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1675 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1730 | 1676 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1731 | 1677 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1732 | 1678 | sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W) |
1733 | 1679 | sage: C.introspect() |
1734 | 1680 | False |
1735 | 1681 | sage: C.evaluate(username='sage') |
1736 | | sage: W.check_comp(9999) |
| 1682 | sage: W.check_comp(9999) # random output -- depends on computer speed |
1737 | 1683 | ('d', Cell 0; in=sage?, out=) |
1738 | 1684 | sage: C.introspect() |
1739 | 1685 | ['sage?', ''] |
1740 | 1686 | sage: C.unset_introspect() |
1741 | 1687 | sage: C.introspect() |
1742 | 1688 | False |
| 1689 | sage: W.quit() |
| 1690 | sage: nb.delete() |
1743 | 1691 | """ |
1744 | 1692 | self.__introspect = False |
1745 | 1693 | |
1746 | 1694 | def set_introspect(self, before_prompt, after_prompt): |
1747 | 1695 | """ |
1748 | 1696 | Set self's introspection text. |
1749 | | |
| 1697 | |
1750 | 1698 | EXAMPLES:: |
1751 | | |
| 1699 | |
1752 | 1700 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1753 | 1701 | sage: C.set_introspect("a", "b") |
1754 | 1702 | sage: C.introspect() |
… |
… |
class Cell(Cell_generic): |
1759 | 1707 | def evaluate(self, introspect=False, time=None, username=None): |
1760 | 1708 | r""" |
1761 | 1709 | INPUT: |
1762 | | |
1763 | | |
| 1710 | |
1764 | 1711 | - ``username`` - name of user doing the evaluation |
1765 | | |
| 1712 | |
1766 | 1713 | - ``time`` - if True return time computation takes |
1767 | | |
| 1714 | |
1768 | 1715 | - ``introspect`` - either False or a pair |
1769 | 1716 | [before_cursor, after_cursor] of strings. |
1770 | | |
1771 | | |
| 1717 | |
1772 | 1718 | EXAMPLES: |
1773 | 1719 | |
1774 | 1720 | We create a notebook, worksheet, and cell and evaluate it |
1775 | 1721 | in order to compute `3^5`:: |
1776 | | |
1777 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1722 | |
| 1723 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1778 | 1724 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1779 | 1725 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1780 | | sage: W.edit_save('Sage\n{{{\n3^5\n}}}') |
| 1726 | sage: W.edit_save('{{{\n3^5\n}}}') |
1781 | 1727 | sage: C = W.cell_list()[0]; C |
1782 | 1728 | Cell 0; in=3^5, out= |
1783 | 1729 | sage: C.evaluate(username='sage') |
1784 | | sage: W.check_comp(wait=9999) |
| 1730 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
1785 | 1731 | ('d', Cell 0; in=3^5, out= |
1786 | 1732 | 243 |
1787 | 1733 | ) |
1788 | | sage: C |
| 1734 | sage: C # random output -- depends on computer speed |
1789 | 1735 | Cell 0; in=3^5, out= |
1790 | | 243 |
1791 | | |
1792 | | :: |
1793 | | |
1794 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 1736 | 243 |
| 1737 | sage: W.quit() |
| 1738 | sage: nb.delete() |
1795 | 1739 | """ |
1796 | 1740 | self.__interrupted = False |
1797 | 1741 | self.__evaluated = True |
… |
… |
class Cell(Cell_generic): |
1810 | 1754 | shutil.rmtree(F) |
1811 | 1755 | except: |
1812 | 1756 | pass |
1813 | | |
| 1757 | |
1814 | 1758 | def version(self): |
1815 | 1759 | """ |
1816 | 1760 | Returns the version number of this cell. |
1817 | | |
| 1761 | |
1818 | 1762 | EXAMPLES:: |
1819 | | |
| 1763 | |
1820 | 1764 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1821 | 1765 | sage: C.version() |
1822 | 1766 | 0 |
… |
… |
class Cell(Cell_generic): |
1834 | 1778 | r""" |
1835 | 1779 | Returns True if the time it takes to evaluate this cell should be |
1836 | 1780 | printed. |
1837 | | |
| 1781 | |
1838 | 1782 | EXAMPLES:: |
1839 | | |
| 1783 | |
1840 | 1784 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1841 | 1785 | sage: C.time() |
1842 | 1786 | False |
… |
… |
class Cell(Cell_generic): |
1874 | 1818 | if div_wrap: |
1875 | 1819 | s = '\n\n<div id="cell_outer_%s" class="cell_visible"><div id="cell_%s" class="%s">'%(self.id(), self.id(), cls) + s + '</div></div>' |
1876 | 1820 | return s |
1877 | | |
| 1821 | |
1878 | 1822 | def html(self, wrap=None, div_wrap=True, do_print=False): |
1879 | 1823 | r""" |
1880 | 1824 | Returns the HTML for self. |
… |
… |
class Cell(Cell_generic): |
1891 | 1835 | |
1892 | 1836 | EXAMPLES:: |
1893 | 1837 | |
1894 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1838 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1895 | 1839 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1896 | 1840 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1897 | 1841 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
… |
… |
class Cell(Cell_generic): |
1922 | 1866 | |
1923 | 1867 | if div_wrap: |
1924 | 1868 | s = '\n\n<div id="cell_outer_%s" class="cell_visible"><div id="cell_%s" class="%s">'%(self.id(), self.id(), cls) + s + '</div></div>' |
1925 | | |
| 1869 | |
1926 | 1870 | #self._html_cache[key] = s |
1927 | 1871 | return s |
1928 | 1872 | |
1929 | 1873 | def html_in(self, do_print=False, ncols=80): |
1930 | 1874 | """ |
1931 | 1875 | Returns the HTML code for the input of this cell. |
1932 | | |
| 1876 | |
1933 | 1877 | EXAMPLES:: |
1934 | | |
| 1878 | |
1935 | 1879 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None) |
1936 | 1880 | sage: print C.html_in() |
1937 | | <div class="insert_new_cell" id="insert_new_cell_0"...</a> |
| 1881 | <div class="insert_new_cell" id="insert_new_cell_before0"...</a> |
1938 | 1882 | """ |
1939 | 1883 | s = '' |
1940 | 1884 | id = self.__id |
1941 | 1885 | t = self.__in.rstrip() |
1942 | 1886 | |
1943 | 1887 | cls = "cell_input_hide" if 'hide' in self.percent_directives() else "cell_input" |
1944 | | |
| 1888 | |
1945 | 1889 | if not do_print: |
1946 | 1890 | s += self.html_new_cell_before() |
1947 | 1891 | |
… |
… |
class Cell(Cell_generic): |
1969 | 1913 | s+= '<a href="javascript:evaluate_cell(%s,0)" class="eval_button" id="eval_button%s" alt="Click here or press shift-return to evaluate">evaluate</a>'%(id,id) |
1970 | 1914 | |
1971 | 1915 | t = escape(t)+" " |
1972 | | |
| 1916 | |
1973 | 1917 | return s |
1974 | 1918 | |
1975 | 1919 | |
… |
… |
class Cell(Cell_generic): |
1978 | 1922 | Returns a notebook URL for this cell. |
1979 | 1923 | |
1980 | 1924 | EXAMPLES:: |
1981 | | |
1982 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1925 | |
| 1926 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1983 | 1927 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1984 | 1928 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1985 | 1929 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
1986 | 1930 | sage: C.url_to_self() |
1987 | 1931 | '/home/sage/0/cells/0' |
1988 | | |
1989 | 1932 | """ |
1990 | 1933 | try: |
1991 | 1934 | return self.__url_to_self |
… |
… |
class Cell(Cell_generic): |
1996 | 1939 | def files(self): |
1997 | 1940 | """ |
1998 | 1941 | Returns a list of all the files in self's directory. |
1999 | | |
| 1942 | |
2000 | 1943 | EXAMPLES:: |
2001 | | |
2002 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1944 | |
| 1945 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
2003 | 1946 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
2004 | 1947 | sage: W = nb.create_new_worksheet('Test', 'sage') |
2005 | 1948 | sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W) |
2006 | 1949 | sage: C.evaluate() |
2007 | | sage: W.check_comp(wait=9999) |
| 1950 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
2008 | 1951 | ('d', Cell 0; in=plot(sin(x),0,5), out= |
2009 | 1952 | <html><font color='black'><img src='cell://sage0.png'></font></html> |
2010 | 1953 | <BLANKLINE> |
2011 | 1954 | ) |
2012 | | sage: C.files() |
| 1955 | sage: C.files() # random output -- depends on computer speed |
2013 | 1956 | ['sage0.png'] |
2014 | | |
2015 | | :: |
2016 | | |
2017 | | sage: import shutil; shutil.rmtree(nb.directory()) |
| 1957 | sage: W.quit() |
| 1958 | sage: nb.delete() |
2018 | 1959 | """ |
2019 | 1960 | dir = self.directory() |
2020 | 1961 | D = os.listdir(dir) |
… |
… |
class Cell(Cell_generic): |
2023 | 1964 | def delete_files(self): |
2024 | 1965 | """ |
2025 | 1966 | Deletes all of the files associated with this cell. |
2026 | | |
| 1967 | |
2027 | 1968 | EXAMPLES:: |
2028 | | |
2029 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1969 | |
| 1970 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
2030 | 1971 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
2031 | 1972 | sage: W = nb.create_new_worksheet('Test', 'sage') |
2032 | 1973 | sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W) |
2033 | 1974 | sage: C.evaluate() |
2034 | | sage: W.check_comp(wait=9999) |
| 1975 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
2035 | 1976 | ('d', Cell 0; in=plot(sin(x),0,5), out= |
2036 | 1977 | <html><font color='black'><img src='cell://sage0.png'></font></html> |
2037 | 1978 | <BLANKLINE> |
2038 | 1979 | ) |
2039 | | sage: C.files() |
| 1980 | sage: C.files() # random output -- depends on computer speed |
2040 | 1981 | ['sage0.png'] |
2041 | 1982 | sage: C.delete_files() |
2042 | 1983 | sage: C.files() |
2043 | 1984 | [] |
| 1985 | sage: W.quit() |
| 1986 | sage: nb.delete() |
2044 | 1987 | """ |
2045 | 1988 | try: |
2046 | 1989 | dir = self._directory_name() |
… |
… |
class Cell(Cell_generic): |
2062 | 2005 | |
2063 | 2006 | EXAMPLES:: |
2064 | 2007 | |
2065 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 2008 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
2066 | 2009 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
2067 | 2010 | sage: W = nb.create_new_worksheet('Test', 'sage') |
2068 | 2011 | sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W) |
2069 | 2012 | sage: C.evaluate() |
2070 | | sage: W.check_comp(wait=9999) |
| 2013 | sage: W.check_comp(wait=9999) # random output -- depends on computer speed |
2071 | 2014 | ('d', Cell 0; in=plot(sin(x),0,5), out= |
2072 | 2015 | <html><font color='black'><img src='cell://sage0.png'></font></html> |
2073 | 2016 | <BLANKLINE> |
2074 | 2017 | ) |
2075 | | sage: C.files_html('') |
| 2018 | sage: C.files_html('') # random output -- depends on computer speed |
2076 | 2019 | '<img src="/home/sage/0/cells/0/sage0.png?...">' |
| 2020 | sage: W.quit() |
| 2021 | sage: nb.delete() |
2077 | 2022 | """ |
2078 | 2023 | import time |
2079 | 2024 | D = self.files() |
… |
… |
class Cell(Cell_generic): |
2113 | 2058 | jmol_file = open(jmol_name, 'r') |
2114 | 2059 | jmol_script = jmol_file.read() |
2115 | 2060 | jmol_file.close() |
2116 | | |
| 2061 | |
2117 | 2062 | jmol_script = jmol_script.replace('defaultdirectory "', 'defaultdirectory "' + self.url_to_self() + '/') |
2118 | 2063 | |
2119 | 2064 | jmol_file = open(jmol_name, 'w') |
2120 | 2065 | jmol_file.write(jmol_script) |
2121 | 2066 | jmol_file.close() |
2122 | | |
| 2067 | |
2123 | 2068 | script = '<div><script>jmol_applet(%s, "%s?%d");</script></div>' % (size, url, time.time()) |
2124 | 2069 | images.append(script) |
2125 | 2070 | elif F.endswith('.jmol.zip'): |
… |
… |
class Cell(Cell_generic): |
2147 | 2092 | Returns the HTML for self's output. |
2148 | 2093 | |
2149 | 2094 | INPUT: |
2150 | | |
| 2095 | |
2151 | 2096 | - ``do_print`` -- a boolean stating whether to output HTML |
2152 | 2097 | for print |
2153 | 2098 | |
… |
… |
class Cell(Cell_generic): |
2155 | 2100 | |
2156 | 2101 | EXAMPLES:: |
2157 | 2102 | |
2158 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 2103 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
2159 | 2104 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
2160 | 2105 | sage: W = nb.create_new_worksheet('Test', 'sage') |
2161 | 2106 | sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W) |
… |
… |
class Cell(Cell_generic): |
2164 | 2109 | """ |
2165 | 2110 | if do_print and self.cell_output_type() == 'hidden': |
2166 | 2111 | return '<pre>\n</pre>' |
2167 | | |
| 2112 | |
2168 | 2113 | out_nowrap = self.output_text(0, html=True) |
2169 | 2114 | |
2170 | 2115 | out_html = self.output_html() |
… |
… |
class Cell(Cell_generic): |
2172 | 2117 | out_wrap = out_nowrap |
2173 | 2118 | else: |
2174 | 2119 | out_wrap = self.output_text(ncols, html=True) |
2175 | | |
| 2120 | |
2176 | 2121 | typ = self.cell_output_type() |
2177 | | |
| 2122 | |
2178 | 2123 | if self.computing(): |
2179 | 2124 | cls = "cell_div_output_running" |
2180 | 2125 | else: |
… |
… |
class Cell(Cell_generic): |
2200 | 2145 | out = out_wrap + out_html |
2201 | 2146 | else: |
2202 | 2147 | out = out_wrap + out_nowrap + out_html |
2203 | | |
| 2148 | |
2204 | 2149 | s = top + out + '</div>' |
2205 | 2150 | |
2206 | 2151 | r = '' |
… |
… |
class Cell(Cell_generic): |
2217 | 2162 | r, s) |
2218 | 2163 | |
2219 | 2164 | return tbl |
2220 | | |
| 2165 | |
2221 | 2166 | |
2222 | 2167 | |
2223 | 2168 | ######## |
… |
… |
class Cell(Cell_generic): |
2225 | 2170 | def format_exception(s0, ncols): |
2226 | 2171 | r""" |
2227 | 2172 | Make it so exceptions don't appear expanded by default. |
2228 | | |
| 2173 | |
2229 | 2174 | INPUT: |
2230 | | |
2231 | | |
2232 | | - ``s0`` - string |
2233 | | |
2234 | | - ``ncols`` - integer |
2235 | | |
2236 | | |
2237 | | OUTPUT: string |
2238 | | |
2239 | | If s0 contains "notracebacks" then this function always returns s0 |
2240 | | |
| 2175 | |
| 2176 | - ``s0`` - string |
| 2177 | |
| 2178 | - ``ncols`` - integer |
| 2179 | |
| 2180 | OUTPUT: |
| 2181 | |
| 2182 | - a string |
| 2183 | |
| 2184 | If s0 contains "notracebacks" then this function always returns |
| 2185 | s0. |
| 2186 | |
2241 | 2187 | EXAMPLES:: |
2242 | | |
| 2188 | |
2243 | 2189 | sage: sagenb.notebook.cell.format_exception(sagenb.notebook.cell.TRACEBACK,80) |
2244 | 2190 | '\nTraceback (click to the left of this block for traceback)\n...\nTraceback (most recent call last):' |
2245 | 2191 | sage: sagenb.notebook.cell.format_exception(sagenb.notebook.cell.TRACEBACK + "notracebacks",80) |
2246 | 2192 | 'Traceback (most recent call last):notracebacks' |
2247 | 2193 | """ |
2248 | 2194 | s = s0.lstrip() |
2249 | | # Add a notracebacks option -- if it is in the string then tracebacks aren't shrunk. |
2250 | | # This is currently used by the sage.server.support.help command. |
| 2195 | # Add a notracebacks option -- if it is in the string then |
| 2196 | # tracebacks aren't shrunk. This is currently used by the |
| 2197 | # sage.server.support.help command. |
2251 | 2198 | if TRACEBACK not in s or 'notracebacks' in s: |
2252 | 2199 | return s0 |
2253 | 2200 | if ncols > 0: |
… |
… |
def format_exception(s0, ncols): |
2261 | 2208 | s = s.replace("exec compile(ur'","") |
2262 | 2209 | s = s.replace("' + '\\n', '', 'single')", "") |
2263 | 2210 | return s |
2264 | | |
| 2211 | |
2265 | 2212 | ComputeCell=Cell |
2266 | 2213 | |
2267 | | |
| 2214 | |
2268 | 2215 | def number_of_rows(txt, ncols): |
2269 | 2216 | r""" |
2270 | 2217 | Returns the number of rows needed to display the string in txt if |
2271 | 2218 | there are a maximum of ncols columns per row. |
2272 | | |
| 2219 | |
2273 | 2220 | EXAMPLES:: |
2274 | | |
| 2221 | |
2275 | 2222 | sage: from sagenb.notebook.cell import number_of_rows |
2276 | 2223 | sage: s = "asdfasdf\nasdfasdf\n" |
2277 | 2224 | sage: number_of_rows(s, 8) |
diff --git a/sagenb/notebook/interact.py b/sagenb/notebook/interact.py
a
|
b
|
class InputBox(InteractControl): |
944 | 944 | EXAMPLES:: |
945 | 945 | |
946 | 946 | sage: sagenb.notebook.interact.InputBox('theta', Color('red'), type=Color)._adaptor('#aaaaaa',globals()) |
947 | | RGB color (0.6640625, 0.6640625, 0.6640625) |
| 947 | RGB color (0.66666666666666663, 0.66666666666666663, 0.66666666666666663) |
948 | 948 | """ |
949 | 949 | if self.__type is None: |
950 | 950 | return sage_eval(value, globs) |
… |
… |
class ColorInput(InputBox): |
1044 | 1044 | EXAMPLES:: |
1045 | 1045 | |
1046 | 1046 | sage: sagenb.notebook.interact.ColorInput('c', Color('red')).render() |
1047 | | '<table>...' |
| 1047 | '...<table>...' |
1048 | 1048 | """ |
1049 | 1049 | return html_color_selector('color-selector-%s-%s'%(self.var(), |
1050 | 1050 | self.cell_id()), |
… |
… |
def interact(f): |
2421 | 2421 | |
2422 | 2422 | sage: v = [] |
2423 | 2423 | sage: html('<h2>Quadratic Root Etch-a-sketch</h2>') |
2424 | | <html><h2>Quadratic Root Etch-a-sketch</h2></html> |
| 2424 | <html>...<h2>Quadratic Root Etch-a-sketch</h2>...</html> |
2425 | 2425 | sage: @interact |
2426 | 2426 | ... def _(a=[-10..10], b=[-10..10], c=[-10..10]): |
2427 | 2427 | ... f = a*x^2 + b*x + c == 0; show(f) |
… |
… |
class color_selector(input_box): |
2723 | 2723 | ... |
2724 | 2724 | ValueError: unknown color 'crayon' |
2725 | 2725 | sage: color_selector('#abcdef', label='height', widget='jpicker') |
2726 | | Interact color selector labeled 'height', with default RGB color (0.66796875, 0.80078125, 0.93359375), widget 'jpicker', and visible input box |
| 2726 | Interact color selector labeled 'height', with default RGB color (0.6705882352941176, 0.80392156862745101, 0.93725490196078431), widget 'jpicker', and visible input box |
2727 | 2727 | sage: color_selector('abcdef', label='height', widget='jpicker') |
2728 | 2728 | Traceback (most recent call last): |
2729 | 2729 | ... |
… |
… |
def list_of_first_n(v,n): |
3588 | 3588 | |
3589 | 3589 | EXAMPLES:: |
3590 | 3590 | |
3591 | | sage: sagenb.notebook.interact.list_of_first_n(Primes(), 10) |
| 3591 | sage: from itertools import takewhile |
| 3592 | sage: p100 = takewhile(lambda x: x < 100, Primes()) |
| 3593 | sage: sagenb.notebook.interact.list_of_first_n(p100, 10) |
3592 | 3594 | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
3593 | 3595 | sage: sagenb.notebook.interact.list_of_first_n((1..5), 10) |
3594 | 3596 | [1, 2, 3, 4, 5] |
diff --git a/sagenb/notebook/notebook.py b/sagenb/notebook/notebook.py
a
|
b
|
USE_REFERENCE_WORKSHEET_PROCESSES = Fals |
21 | 21 | # System libraries |
22 | 22 | import os |
23 | 23 | import random |
24 | | import re |
| 24 | import re |
25 | 25 | import shutil |
26 | 26 | import socket |
27 | 27 | import time |
… |
… |
class Notebook(object): |
100 | 100 | for username in self.__users.keys(): |
101 | 101 | for w in S.worksheets(username): |
102 | 102 | W['%s/%s'%(username, w.id_number())] = w |
103 | | |
| 103 | |
104 | 104 | self.__worksheets = W |
105 | | |
| 105 | |
106 | 106 | def delete(self): |
107 | 107 | """ |
108 | 108 | Delete all files related to this notebook. |
109 | | |
| 109 | |
110 | 110 | This is used for doctesting mainly. This command is obviously |
111 | 111 | *VERY* dangerous to use on a notebook you actually care about. |
112 | 112 | You could easily lose all data. |
113 | | |
| 113 | |
114 | 114 | EXAMPLES:: |
115 | | |
116 | | sage: tmp = tmp_dir() |
117 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp) |
118 | | sage: sorted(os.listdir(tmp)) |
119 | | ['backups', 'nb2.sobj', 'objects', 'worksheets'] |
| 115 | |
| 116 | sage: tmp = tmp_dir() + '.sagenb' |
| 117 | sage: nb = sagenb.notebook.notebook.Notebook(tmp) |
| 118 | sage: sorted(os.listdir(tmp)) |
| 119 | ['home'] |
120 | 120 | sage: nb.delete() |
121 | | |
122 | | Now the directory is gone. |
123 | | |
124 | | :: |
125 | | |
| 121 | |
| 122 | Now the directory is gone.:: |
| 123 | |
126 | 124 | sage: os.listdir(tmp) |
127 | 125 | Traceback (most recent call last): |
128 | 126 | ... |
… |
… |
class Notebook(object): |
135 | 133 | |
136 | 134 | def system_names(self): |
137 | 135 | return SYSTEM_NAMES |
138 | | |
| 136 | |
139 | 137 | ########################################################## |
140 | 138 | # Users |
141 | 139 | ########################################################## |
142 | 140 | def create_default_users(self, passwd): |
143 | 141 | """ |
144 | 142 | Create the default users for a notebook. |
145 | | |
| 143 | |
146 | 144 | INPUT: |
147 | 145 | |
148 | 146 | - ``passwd`` - a string |
149 | | |
| 147 | |
150 | 148 | EXAMPLES:: |
151 | | |
152 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 149 | |
| 150 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
153 | 151 | sage: nb.create_default_users('password') |
154 | 152 | Creating default users. |
155 | 153 | sage: list(sorted(nb.users().iteritems())) |
… |
… |
class Notebook(object): |
174 | 172 | def user_exists(self, username): |
175 | 173 | """ |
176 | 174 | Return whether a user with the given ``username`` exists. |
177 | | |
| 175 | |
178 | 176 | INPUT: |
179 | 177 | |
180 | 178 | - ``username`` - a string |
… |
… |
class Notebook(object): |
184 | 182 | - a bool |
185 | 183 | |
186 | 184 | EXAMPLES:: |
187 | | |
188 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 185 | |
| 186 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
189 | 187 | sage: nb.create_default_users('password') |
190 | 188 | Creating default users. |
191 | 189 | sage: nb.user_exists('admin') |
… |
… |
class Notebook(object): |
198 | 196 | True |
199 | 197 | """ |
200 | 198 | return username in self.users() |
201 | | |
| 199 | |
202 | 200 | def users(self): |
203 | 201 | """ |
204 | 202 | Return a dictionary of users in a notebook. |
205 | | |
| 203 | |
206 | 204 | OUTPUT: |
207 | 205 | |
208 | 206 | - a string:User instance dictionary |
209 | 207 | |
210 | 208 | EXAMPLES:: |
211 | | |
212 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 209 | |
| 210 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
213 | 211 | sage: nb.create_default_users('password') |
214 | 212 | Creating default users. |
215 | 213 | sage: list(sorted(nb.users().iteritems())) |
… |
… |
class Notebook(object): |
225 | 223 | """ |
226 | 224 | Return an instance of the User class given the ``username`` of a user |
227 | 225 | in a notebook. |
228 | | |
| 226 | |
229 | 227 | INPUT: |
230 | 228 | |
231 | 229 | - ``username`` - a string |
… |
… |
class Notebook(object): |
235 | 233 | - an instance of User |
236 | 234 | |
237 | 235 | EXAMPLES:: |
238 | | |
239 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 236 | |
| 237 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
240 | 238 | sage: nb.create_default_users('password') |
241 | 239 | Creating default users. |
242 | 240 | sage: nb.user('admin') |
243 | 241 | admin |
244 | | sage: nb.user('admin')._User__email |
| 242 | sage: nb.user('admin').get_email() |
245 | 243 | '' |
246 | | sage: nb.user('admin')._User__password |
| 244 | sage: nb.user('admin').password() |
247 | 245 | 'aajfMKNH1hTm2' |
248 | 246 | """ |
249 | 247 | if not isinstance(username, str) or '/' in username: |
… |
… |
class Notebook(object): |
265 | 263 | def create_user_with_same_password(self, user, other_user): |
266 | 264 | r""" |
267 | 265 | Change the password of ``user`` to that of ``other_user``. |
268 | | |
| 266 | |
269 | 267 | INPUT: |
270 | | |
| 268 | |
271 | 269 | - ``user`` - a string |
272 | | |
| 270 | |
273 | 271 | - ``other_user`` - a string |
274 | | |
| 272 | |
275 | 273 | EXAMPLES:: |
276 | | |
277 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 274 | |
| 275 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
278 | 276 | sage: nb.add_user('bob', 'an**d', 'bob@gmail.com', force=True) |
279 | 277 | sage: nb.user('bob').password() |
280 | 278 | 'aa4Q6Jbx/MiUs' |
… |
… |
class Notebook(object): |
297 | 295 | INPUT: |
298 | 296 | |
299 | 297 | - ``user`` - an instance of User |
300 | | |
| 298 | |
301 | 299 | OUTPUT: |
302 | | |
| 300 | |
303 | 301 | - a bool |
304 | 302 | |
305 | 303 | EXAMPLES:: |
306 | | |
307 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 304 | |
| 305 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
308 | 306 | sage: nb.add_user('Administrator', 'password', '', 'admin', True) |
309 | 307 | sage: nb.add_user('RegularUser', 'password', '', 'user', True) |
310 | 308 | sage: nb.user_is_admin('Administrator') |
… |
… |
class Notebook(object): |
321 | 319 | INPUT: |
322 | 320 | |
323 | 321 | - ``username`` - a string |
324 | | |
| 322 | |
325 | 323 | OUTPUT: |
326 | 324 | |
327 | 325 | - a bool |
328 | 326 | |
329 | 327 | EXAMPLES:: |
330 | | |
331 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 328 | |
| 329 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
332 | 330 | sage: nb.create_default_users('password') |
333 | 331 | Creating default users. |
334 | 332 | sage: nb.user_is_guest('guest') |
… |
… |
class Notebook(object): |
340 | 338 | return self.user(username).is_guest() |
341 | 339 | except KeyError: |
342 | 340 | return False |
343 | | |
| 341 | |
344 | 342 | def user_list(self): |
345 | 343 | """ |
346 | 344 | Return a list of user objects. |
347 | | |
| 345 | |
348 | 346 | OUTPUT: |
349 | 347 | |
350 | 348 | - a list of User instances |
351 | 349 | |
352 | 350 | EXAMPLES:: |
353 | | |
354 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 351 | |
| 352 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
355 | 353 | sage: nb.create_default_users('password') |
356 | 354 | Creating default users. |
357 | 355 | sage: sorted(nb.user_list(), key=lambda k: k.username()) |
… |
… |
class Notebook(object): |
362 | 360 | def usernames(self): |
363 | 361 | """ |
364 | 362 | Return a list of usernames. |
365 | | |
| 363 | |
366 | 364 | OUTPUT: |
367 | 365 | |
368 | 366 | - a list of strings |
369 | 367 | |
370 | 368 | EXAMPLES:: |
371 | | |
372 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 369 | |
| 370 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
373 | 371 | sage: nb.create_default_users('password') |
374 | 372 | Creating default users. |
375 | 373 | sage: sorted(nb.usernames()) |
… |
… |
class Notebook(object): |
381 | 379 | def valid_login_names(self): |
382 | 380 | """ |
383 | 381 | Return a list of users that can log in. |
384 | | |
| 382 | |
385 | 383 | OUTPUT: |
386 | 384 | |
387 | 385 | - a list of strings |
388 | 386 | |
389 | 387 | EXAMPLES:: |
390 | | |
391 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 388 | |
| 389 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
392 | 390 | sage: nb.create_default_users('password') |
393 | 391 | Creating default users. |
394 | 392 | sage: nb.valid_login_names() |
… |
… |
class Notebook(object): |
407 | 405 | confronted with the Sage notebook login page. Currently, this |
408 | 406 | returns 'admin' if that is the *only* user. Otherwise it |
409 | 407 | returns an empty string (''). |
410 | | |
| 408 | |
411 | 409 | OUTPUT: |
412 | 410 | |
413 | | - a string - the default username. |
414 | | |
| 411 | - a string - the default username. |
| 412 | |
415 | 413 | EXAMPLES:: |
416 | | |
417 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 414 | |
| 415 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
418 | 416 | sage: nb.create_default_users('password') |
419 | 417 | Creating default users. |
420 | 418 | sage: nb.default_user() |
… |
… |
class Notebook(object): |
433 | 431 | Set the accounts attribute of the server configuration to |
434 | 432 | ``value``. This property determines whether users can create |
435 | 433 | new accounts. |
436 | | |
| 434 | |
437 | 435 | INPUT: |
438 | | |
| 436 | |
439 | 437 | - ``value`` - a bool |
440 | 438 | |
441 | 439 | EXAMPLES:: |
442 | | |
443 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 440 | |
| 441 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
444 | 442 | sage: nb.get_accounts() |
445 | 443 | False |
446 | 444 | sage: nb.set_accounts(True) |
… |
… |
class Notebook(object): |
454 | 452 | |
455 | 453 | def get_accounts(self): |
456 | 454 | r""" |
457 | | Return whether or not users can create new accounts. |
458 | | |
| 455 | Return whether or not users can create new accounts. |
| 456 | |
459 | 457 | OUTPUT: |
460 | 458 | |
461 | 459 | - a bool |
462 | 460 | |
463 | 461 | EXAMPLES:: |
464 | | |
465 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 462 | |
| 463 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
466 | 464 | sage: nb.get_accounts() |
467 | 465 | False |
468 | 466 | sage: nb.set_accounts(True) |
… |
… |
class Notebook(object): |
476 | 474 | Add a user with the given credentials. |
477 | 475 | |
478 | 476 | INPUT: |
479 | | |
| 477 | |
480 | 478 | - ``username`` - the username |
481 | | |
| 479 | |
482 | 480 | - ``password`` - the password |
483 | | |
| 481 | |
484 | 482 | - ``email`` - the email address |
485 | | |
| 483 | |
486 | 484 | - ``account_type`` - one of 'user', 'admin', or 'guest' |
487 | | |
| 485 | |
488 | 486 | - ``force`` - a bool (default: False) |
489 | | |
| 487 | |
490 | 488 | If the method :meth:`get_accounts` returns False then user can |
491 | 489 | only be added if ``force`` is True. |
492 | 490 | |
493 | 491 | EXAMPLES:: |
494 | | |
495 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 492 | |
| 493 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
496 | 494 | sage: nb.add_user('Mark', 'password', '', force=True) |
497 | 495 | sage: nb.user('Mark') |
498 | 496 | Mark |
… |
… |
class Notebook(object): |
506 | 504 | """ |
507 | 505 | if not self.get_accounts() and not force: |
508 | 506 | raise ValueError, "creating new accounts disabled." |
509 | | |
| 507 | |
510 | 508 | us = self.users() |
511 | 509 | if us.has_key(username): |
512 | 510 | print "WARNING: User '%s' already exists -- and is now being replaced."%username |
… |
… |
class Notebook(object): |
515 | 513 | |
516 | 514 | # Save the user database |
517 | 515 | self.__storage.save_users(self.users()) |
518 | | |
| 516 | |
519 | 517 | |
520 | 518 | def change_password(self, username, password): |
521 | 519 | """ |
522 | 520 | Change a user's password. |
523 | 521 | |
524 | 522 | INPUT: |
525 | | |
| 523 | |
526 | 524 | - ``username`` - a string, the username |
527 | | |
| 525 | |
528 | 526 | - ``password`` - a string, the user's new password |
529 | | |
| 527 | |
530 | 528 | EXAMPLES:: |
531 | | |
532 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 529 | |
| 530 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
533 | 531 | sage: nb.add_user('Mark', 'password', '', force=True) |
534 | 532 | sage: nb.user('Mark').password() |
535 | 533 | 'aajfMKNH1hTm2' |
… |
… |
class Notebook(object): |
542 | 540 | def del_user(self, username): |
543 | 541 | """ |
544 | 542 | Delete the given user. |
545 | | |
| 543 | |
546 | 544 | INPUT: |
547 | 545 | |
548 | 546 | - ``username`` - a string |
549 | 547 | |
550 | 548 | EXAMPLES:: |
551 | | |
552 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 549 | |
| 550 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
553 | 551 | sage: nb.add_user('Mark', 'password', '', force=True) |
554 | 552 | sage: nb.user('Mark') |
555 | 553 | Mark |
… |
… |
class Notebook(object): |
565 | 563 | def passwords(self): |
566 | 564 | """ |
567 | 565 | Return a username:password dictionary. |
568 | | |
| 566 | |
569 | 567 | OUTPUT: |
570 | 568 | |
571 | 569 | - a string:string dictionary |
572 | 570 | |
573 | 571 | EXAMPLES:: |
574 | | |
575 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 572 | |
| 573 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
576 | 574 | sage: nb.create_default_users('password') |
577 | 575 | Creating default users. |
578 | 576 | sage: nb.add_user('Mark', 'password', '', force=True) |
… |
… |
class Notebook(object): |
584 | 582 | def user_conf(self, username): |
585 | 583 | """ |
586 | 584 | Return a user's configuration object. |
587 | | |
| 585 | |
588 | 586 | OUTPUT: |
589 | 587 | |
590 | 588 | - an instance of Configuration. |
591 | 589 | |
592 | 590 | EXAMPLES:: |
593 | | |
594 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 591 | |
| 592 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
595 | 593 | sage: nb.create_default_users('password') |
596 | 594 | Creating default users. |
597 | 595 | sage: config = nb.user_conf('admin') |
598 | 596 | sage: config['max_history_length'] |
599 | 597 | 1000 |
600 | | sage: config['system'] |
| 598 | sage: config['default_system'] |
601 | 599 | 'sage' |
602 | 600 | sage: config['autosave_interval'] |
603 | 601 | 3600 |
604 | 602 | sage: config['default_pretty_print'] |
605 | 603 | False |
606 | | """ |
| 604 | """ |
607 | 605 | return self.users()[username].conf() |
608 | 606 | |
609 | 607 | ########################################################## |
… |
… |
class Notebook(object): |
639 | 637 | r""" |
640 | 638 | Publish a user's worksheet. This creates a new worksheet in |
641 | 639 | the 'pub' directory with the same contents as ``worksheet``. |
642 | | |
| 640 | |
643 | 641 | INPUT: |
644 | 642 | |
645 | 643 | - ``worksheet`` - an instance of Worksheet |
… |
… |
class Notebook(object): |
651 | 649 | - a new or existing published instance of Worksheet |
652 | 650 | |
653 | 651 | EXAMPLES:: |
654 | | |
655 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 652 | |
| 653 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
656 | 654 | sage: nb.add_user('Mark','password','',force=True) |
657 | 655 | sage: W = nb.new_worksheet_with_title_from_text('First steps', owner='Mark') |
658 | 656 | sage: nb.worksheet_names() |
659 | 657 | ['Mark/0'] |
660 | 658 | sage: nb.publish_worksheet(nb.get_worksheet_with_filename('Mark/0'), 'Mark') |
661 | | <BLANKLINE> |
662 | | [Cell 0; in=, out=] |
| 659 | pub/1: [Cell 1; in=, out=] |
663 | 660 | sage: sorted(nb.worksheet_names()) |
664 | | ['Mark/0', 'pub/0'] |
| 661 | ['Mark/0', 'pub/1'] |
665 | 662 | """ |
666 | 663 | for X in self.__worksheets.itervalues(): |
667 | 664 | if X.is_published() and X.worksheet_that_was_published() == worksheet: |
… |
… |
class Notebook(object): |
753 | 750 | def empty_trash(self, username): |
754 | 751 | """ |
755 | 752 | Empty the trash for the given user. |
756 | | |
| 753 | |
757 | 754 | INPUT: |
758 | | |
| 755 | |
759 | 756 | - ``username`` - a string |
760 | | |
| 757 | |
761 | 758 | This empties the trash for the given user and cleans up all files |
762 | 759 | associated with the worksheets that are in the trash. |
763 | | |
| 760 | |
764 | 761 | EXAMPLES:: |
765 | | |
766 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 762 | |
| 763 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
767 | 764 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
768 | 765 | sage: W = nb.new_worksheet_with_title_from_text('Sage', owner='sage') |
769 | 766 | sage: W.move_to_trash('sage') |
… |
… |
class Notebook(object): |
783 | 780 | def worksheet_names(self): |
784 | 781 | """ |
785 | 782 | Return a list of all the names of worksheets in this notebook. |
786 | | |
| 783 | |
787 | 784 | OUTPUT: |
788 | 785 | |
789 | 786 | - a list of strings. |
790 | | |
791 | | EXAMPLES: |
| 787 | |
| 788 | EXAMPLES: |
792 | 789 | |
793 | 790 | We make a new notebook with two users and two worksheets, |
794 | 791 | then list their names:: |
795 | | |
796 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 792 | |
| 793 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
797 | 794 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
798 | 795 | sage: W = nb.new_worksheet_with_title_from_text('Sage', owner='sage') |
799 | 796 | sage: nb.add_user('wstein','sage','wstein@sagemath.org',force=True) |
800 | 797 | sage: W2 = nb.new_worksheet_with_title_from_text('Elliptic Curves', owner='wstein') |
801 | 798 | sage: nb.worksheet_names() |
802 | | ['sage/0', 'wstein/0'] |
| 799 | ['sage/0', 'wstein/1'] |
803 | 800 | """ |
804 | 801 | W = self.__worksheets.keys() |
805 | 802 | W.sort() |
… |
… |
class Notebook(object): |
808 | 805 | |
809 | 806 | ########################################################## |
810 | 807 | # Information about the pool of worksheet compute servers |
811 | | ########################################################## |
| 808 | ########################################################## |
812 | 809 | |
813 | 810 | def server_pool(self): |
814 | 811 | return self.conf()['server_pool'] |
… |
… |
class Notebook(object): |
853 | 850 | ulimit = self.get_ulimit() |
854 | 851 | from sagenb.interfaces import ProcessLimits |
855 | 852 | # We have to parse the ulimit format to our ProcessLimits. |
856 | | # The typical format is. |
| 853 | # The typical format is. |
857 | 854 | # '-u 400 -v 1000000 -t 3600' |
858 | 855 | # Despite -t being cputime for ulimit, we map it to walltime, |
859 | 856 | # since that is the only thing that really makes sense for a |
… |
… |
class Notebook(object): |
869 | 866 | if x.startswith(k): tbl[k] = int(x.split()[1].strip()) |
870 | 867 | if tbl['v'] is not None: |
871 | 868 | tbl['v'] = tbl['v']/1000.0 |
872 | | |
873 | | |
| 869 | |
| 870 | |
874 | 871 | process_limits = ProcessLimits(max_vmem=tbl['v'], max_walltime=tbl['t'], |
875 | 872 | max_processes=tbl['u']) |
876 | | |
| 873 | |
877 | 874 | server_pool = self.server_pool() |
878 | 875 | if not server_pool or len(server_pool) == 0: |
879 | 876 | return WorksheetProcess_ExpectImplementation(process_limits=process_limits) |
… |
… |
class Notebook(object): |
884 | 881 | return WorksheetProcess_RemoteExpectImplementation(user_at_host=user_at_host, |
885 | 882 | process_limits=process_limits, |
886 | 883 | remote_python=python_command) |
887 | | |
888 | | |
| 884 | |
| 885 | |
889 | 886 | def _python_command(self): |
890 | 887 | """ |
891 | 888 | """ |
892 | 889 | try: return self.__python_command |
893 | 890 | except AttributeError: pass |
894 | | |
895 | | |
896 | | |
| 891 | |
| 892 | |
| 893 | |
897 | 894 | ########################################################## |
898 | 895 | # The default math software system for new worksheets for |
899 | 896 | # a given user or the whole notebook (if username is None). |
900 | 897 | ########################################################## |
901 | | |
| 898 | |
902 | 899 | def system(self, username=None): |
903 | 900 | return self.user(username).conf()['default_system'] |
904 | 901 | |
… |
… |
class Notebook(object): |
906 | 903 | # The default typeset setting for new worksheets for |
907 | 904 | # a given user or the whole notebook (if username is None). |
908 | 905 | ########################################################## |
909 | | |
| 906 | |
910 | 907 | # TODO -- only implemented for the notebook right now |
911 | 908 | def pretty_print(self, username=None): |
912 | 909 | return self.user(username).conf()['default_pretty_print'] |
… |
… |
class Notebook(object): |
947 | 944 | W = self.create_new_worksheet(name, username) |
948 | 945 | W.edit_save('Log Worksheet\n' + self.user_history_text(username, maxlen=None)) |
949 | 946 | return W |
950 | | |
| 947 | |
951 | 948 | def user_history_text(self, username, maxlen=None): |
952 | 949 | history = self.user_history(username) |
953 | 950 | if maxlen: |
… |
… |
class Notebook(object): |
968 | 965 | def export_worksheet(self, worksheet_filename, output_filename, title=None): |
969 | 966 | """ |
970 | 967 | Export a worksheet, creating a sws file on the file system. |
971 | | |
| 968 | |
972 | 969 | INPUT: |
973 | | |
| 970 | |
974 | 971 | - ``worksheet_filename`` - a string e.g., 'username/id_number' |
975 | | |
| 972 | |
976 | 973 | - ``output_filename`` - a string, e.g., 'worksheet.sws' |
977 | 974 | |
978 | 975 | - ``title`` - title to use for the exported worksheet (if |
… |
… |
class Notebook(object): |
998 | 995 | - ``id_number`` - nonnegative integer or None (default) |
999 | 996 | """ |
1000 | 997 | S = self.__storage |
1001 | | if id_number is None: |
| 998 | if id_number is None: |
1002 | 999 | id_number = self.new_id_number(username) |
1003 | 1000 | return S.load_worksheet(username, id_number) |
1004 | | |
| 1001 | |
1005 | 1002 | def new_id_number(self, username): |
1006 | 1003 | """ |
1007 | 1004 | Find the next worksheet id for the given user. |
… |
… |
class Notebook(object): |
1029 | 1026 | Import a worksheet with the given ``filename`` and set its |
1030 | 1027 | ``owner``. If the file extension is not txt or sws, raise a |
1031 | 1028 | ValueError. |
1032 | | |
| 1029 | |
1033 | 1030 | INPUT: |
1034 | | |
| 1031 | |
1035 | 1032 | - ``filename`` - a string |
1036 | | |
| 1033 | |
1037 | 1034 | - ``owner`` - a string |
1038 | | |
| 1035 | |
1039 | 1036 | OUTPUT: |
1040 | | |
| 1037 | |
1041 | 1038 | - ``worksheet`` - a newly created Worksheet instance |
1042 | | |
1043 | | EXAMPLES: |
| 1039 | |
| 1040 | EXAMPLES: |
1044 | 1041 | |
1045 | 1042 | We create a notebook and import a plain text worksheet |
1046 | 1043 | into it. |
1047 | | |
| 1044 | |
1048 | 1045 | :: |
1049 | | |
1050 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
1051 | | sage: name = tmp_filename() + '.txt' |
| 1046 | |
| 1047 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
| 1048 | sage: name = tmp_filename() + '.txt' |
1052 | 1049 | sage: open(name,'w').write('foo\n{{{\n2+3\n}}}') |
1053 | 1050 | sage: W = nb.import_worksheet(name, 'admin') |
1054 | | |
| 1051 | |
1055 | 1052 | W is our newly-created worksheet, with the 2+3 cell in it:: |
1056 | | |
| 1053 | |
1057 | 1054 | sage: W.name() |
1058 | 1055 | 'foo' |
1059 | 1056 | sage: W.cell_list() |
1060 | | [Cell 0; in=2+3, out=] |
| 1057 | [TextCell 0: foo, Cell 1; in=2+3, out=] |
1061 | 1058 | """ |
1062 | 1059 | if not os.path.exists(filename): |
1063 | 1060 | raise ValueError, "no file %s"%filename |
… |
… |
class Notebook(object): |
1075 | 1072 | raise ValueError, "unknown extension '%s'"%ext |
1076 | 1073 | self.__worksheets[W.filename()] = W |
1077 | 1074 | return W |
1078 | | |
| 1075 | |
1079 | 1076 | def _import_worksheet_txt(self, filename, owner): |
1080 | 1077 | r""" |
1081 | 1078 | Import a plain text file as a new worksheet. |
1082 | | |
| 1079 | |
1083 | 1080 | INPUT: |
1084 | | |
| 1081 | |
1085 | 1082 | - ``filename`` - a string; a filename that ends in .txt |
1086 | | |
| 1083 | |
1087 | 1084 | - ``owner`` - a string; the imported worksheet's owner |
1088 | | |
| 1085 | |
1089 | 1086 | OUTPUT: |
1090 | 1087 | |
1091 | 1088 | - a new instance of Worksheet |
1092 | | |
1093 | | EXAMPLES: |
| 1089 | |
| 1090 | EXAMPLES: |
1094 | 1091 | |
1095 | 1092 | We write a plain text worksheet to a file and import it |
1096 | | using this function. |
1097 | | |
1098 | | :: |
1099 | | |
1100 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
1101 | | sage: name = tmp_filename() + '.txt' |
| 1093 | using this function.:: |
| 1094 | |
| 1095 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
| 1096 | sage: name = tmp_filename() + '.txt' |
1102 | 1097 | sage: open(name,'w').write('foo\n{{{\na = 10\n}}}') |
1103 | 1098 | sage: W = nb._import_worksheet_txt(name, 'admin'); W |
1104 | | [Cell 0; in=a = 10, out=] |
| 1099 | admin/0: [TextCell 0: foo, Cell 1; in=a = 10, out=] |
1105 | 1100 | """ |
1106 | 1101 | # Open the worksheet txt file and load it in. |
1107 | 1102 | worksheet_txt = open(filename).read() |
… |
… |
class Notebook(object): |
1110 | 1105 | # Set the new worksheet to have the contents specified by that file. |
1111 | 1106 | worksheet.edit_save(worksheet_txt) |
1112 | 1107 | return worksheet |
1113 | | |
1114 | | def _import_worksheet_sws(self, filename, username, verbose=True): |
| 1108 | |
| 1109 | def _import_worksheet_sws(self, filename, username): |
1115 | 1110 | r""" |
1116 | 1111 | Import an sws format worksheet into this notebook as a new |
1117 | | worksheet. If the worksheet cannot be read, raise a |
1118 | | ValueError. |
1119 | | |
| 1112 | worksheet. |
| 1113 | |
1120 | 1114 | INPUT: |
1121 | | |
| 1115 | |
1122 | 1116 | - ``filename`` - a string; a filename that ends in .sws; |
1123 | 1117 | internally it must be a tar'd bz2'd file. |
1124 | | |
| 1118 | |
1125 | 1119 | - ``username`` - a string |
1126 | | |
1127 | | - ``verbose`` - a bool (default: True) if True print some the |
1128 | | tar command used to extract the sws file. |
1129 | | |
| 1120 | |
1130 | 1121 | OUTPUT: |
1131 | 1122 | |
1132 | 1123 | - a new Worksheet instance |
1133 | | |
1134 | | EXAMPLES: We create a notebook, then make a worksheet from a plain |
1135 | | text file first. |
1136 | | |
1137 | | :: |
1138 | | |
1139 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
1140 | | sage: name = tmp_filename() + '.txt' |
1141 | | sage: open(name,'w').write('foo\n{{{\n2+3\n}}}') |
| 1124 | |
| 1125 | EXAMPLES: |
| 1126 | |
| 1127 | We create a notebook, then make a worksheet from a plain text |
| 1128 | file first.:: |
| 1129 | |
| 1130 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
| 1131 | sage: name = tmp_filename() + '.txt' |
| 1132 | sage: open(name,'w').write('{{{id=0\n2+3\n}}}') |
1142 | 1133 | sage: W = nb.import_worksheet(name, 'admin') |
1143 | 1134 | sage: W.filename() |
1144 | 1135 | 'admin/0' |
1145 | | |
1146 | | We then export the worksheet to an sws file. |
1147 | | |
1148 | | :: |
1149 | | |
1150 | | sage: nb.export_worksheet(W.filename(), 'tmp.sws', verbose=False) |
1151 | | |
1152 | | Now we import the sws. |
1153 | | |
1154 | | :: |
1155 | | |
1156 | | sage: nb._import_worksheet_sws('tmp.sws', 'admin', verbose=False) |
1157 | | [Cell 0; in=2+3, out=] |
1158 | | |
| 1136 | |
| 1137 | We then export the worksheet to an sws file.:: |
| 1138 | |
| 1139 | sage: sws = os.path.join(tmp_dir(), 'tmp.sws') |
| 1140 | sage: nb.export_worksheet(W.filename(), sws) |
| 1141 | |
| 1142 | Now we import the sws.:: |
| 1143 | |
| 1144 | sage: W = nb._import_worksheet_sws(sws, 'admin') |
| 1145 | sage: nb._Notebook__worksheets[W.filename()] = W |
| 1146 | |
1159 | 1147 | Yes, it's there now (as admin/2):: |
1160 | | |
| 1148 | |
1161 | 1149 | sage: nb.worksheet_names() |
1162 | | ['admin/0', 'admin/2'] |
| 1150 | ['admin/0', 'admin/1'] |
1163 | 1151 | """ |
1164 | 1152 | id_number = self.new_id_number(username) |
1165 | 1153 | worksheet = self.__storage.import_worksheet(username, id_number, filename) |
… |
… |
class Notebook(object): |
1168 | 1156 | # support multiple worksheets with the same title very well |
1169 | 1157 | # already. So it's commented out. |
1170 | 1158 | # self.change_worksheet_name_to_avoid_collision(worksheet) |
1171 | | |
| 1159 | |
1172 | 1160 | return worksheet |
1173 | 1161 | |
1174 | 1162 | def change_worksheet_name_to_avoid_collision(self, worksheet): |
… |
… |
class Notebook(object): |
1187 | 1175 | i += 1 |
1188 | 1176 | name = name + " (%s)"%i |
1189 | 1177 | worksheet.set_name(name) |
1190 | | |
| 1178 | |
1191 | 1179 | |
1192 | 1180 | ########################################################## |
1193 | 1181 | # Importing and exporting worksheets to a plain text format |
… |
… |
class Notebook(object): |
1198 | 1186 | Return HTML containing the plain text version of a worksheet. |
1199 | 1187 | |
1200 | 1188 | INPUT: |
1201 | | |
| 1189 | |
1202 | 1190 | - ``filename`` - a string; filename of a worksheet |
1203 | | |
| 1191 | |
1204 | 1192 | - ``prompts`` - a bool (default: True); whether to format the |
1205 | 1193 | text for inclusion in docstrings |
1206 | 1194 | |
1207 | 1195 | OUTPUT: |
1208 | | |
| 1196 | |
1209 | 1197 | - a string - the worksheet's HTML representation |
1210 | 1198 | """ |
1211 | 1199 | worksheet = self.get_worksheet_with_filename(filename) |
… |
… |
class Notebook(object): |
1249 | 1237 | for W in self.__worksheets.itervalues(): |
1250 | 1238 | if W.docbrowser() and W.compute_process_has_been_started(): |
1251 | 1239 | W.quit_if_idle(DOC_TIMEOUT) |
1252 | | return |
| 1240 | return |
1253 | 1241 | |
1254 | 1242 | for W in self.__worksheets.itervalues(): |
1255 | 1243 | if W.compute_process_has_been_started(): |
1256 | 1244 | W.quit_if_idle(timeout) |
1257 | | |
| 1245 | |
1258 | 1246 | |
1259 | 1247 | ########################################################## |
1260 | 1248 | # Worksheet HTML generation |
… |
… |
class Notebook(object): |
1264 | 1252 | Return the HTML for a given worksheet. |
1265 | 1253 | |
1266 | 1254 | INPUT: |
1267 | | |
| 1255 | |
1268 | 1256 | - ``filename`` - a string; the worksheet's filename |
1269 | 1257 | |
1270 | 1258 | - ``do_print`` - a bool (default: False); whether this is a |
1271 | 1259 | printed worksheet |
1272 | 1260 | |
1273 | 1261 | OUTPUT: |
1274 | | |
| 1262 | |
1275 | 1263 | - a string - the worksheet rendered as HTML |
1276 | 1264 | |
1277 | 1265 | EXAMPLES:: |
1278 | | |
1279 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1266 | |
| 1267 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
1280 | 1268 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1281 | 1269 | sage: nb.worksheet_html(W.filename()) |
1282 | 1270 | '...<!D...Test...cell_id_list=[1]...</body>...</html>' |
… |
… |
class Notebook(object): |
1287 | 1275 | worksheet_html = worksheet.html(include_title=False, do_print=do_print), |
1288 | 1276 | do_print = do_print) |
1289 | 1277 | |
1290 | | |
| 1278 | |
1291 | 1279 | |
1292 | 1280 | def worksheet_list_for_public(self, username, sort='last_edited', reverse=False, search=None): |
1293 | 1281 | W = [x for x in self.__worksheets.itervalues() if x.is_published() and not x.is_trashed(user)] |
… |
… |
class Notebook(object): |
1319 | 1307 | Return HTML for the revision list of a worksheet. |
1320 | 1308 | |
1321 | 1309 | INPUT: |
1322 | | |
| 1310 | |
1323 | 1311 | - ``username`` - a string |
1324 | | |
| 1312 | |
1325 | 1313 | - ``worksheet`` - an instance of Worksheet |
1326 | 1314 | |
1327 | 1315 | OUTPUT: |
… |
… |
class Notebook(object): |
1329 | 1317 | - a string - the HTML for the revision list |
1330 | 1318 | |
1331 | 1319 | EXAMPLES:: |
1332 | | |
1333 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1320 | |
| 1321 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1334 | 1322 | sage: W = nb.create_new_worksheet('Test', 'admin') |
| 1323 | sage: W.body() |
| 1324 | '\n\n{{{id=1|\n\n///\n}}}' |
| 1325 | sage: W.save_snapshot('admin') |
1335 | 1326 | sage: nb.html_worksheet_revision_list('admin', W) |
1336 | 1327 | '...Revision...Last Edited...seconds...ago...' |
1337 | 1328 | """ |
… |
… |
class Notebook(object): |
1349 | 1340 | Return the HTML for a specific revision of a worksheet. |
1350 | 1341 | |
1351 | 1342 | INPUT: |
1352 | | |
| 1343 | |
1353 | 1344 | - ``username`` - a string |
1354 | | |
| 1345 | |
1355 | 1346 | - ``ws`` - an instance of Worksheet |
1356 | | |
| 1347 | |
1357 | 1348 | - ``rev`` - a string containing the key of the revision |
1358 | 1349 | |
1359 | 1350 | OUTPUT: |
1360 | | |
| 1351 | |
1361 | 1352 | - a string - the revision rendered as HTML |
1362 | 1353 | """ |
1363 | 1354 | t = time.time() - float(rev[:-4]) |
1364 | 1355 | time_ago = worksheet.convert_seconds_to_meaningful_time_span(t) |
1365 | | |
| 1356 | |
1366 | 1357 | filename = ws.get_snapshot_text_filename(rev) |
1367 | 1358 | txt = bz2.decompress(open(filename).read()) |
1368 | 1359 | W = self.scratch_worksheet() |
… |
… |
class Notebook(object): |
1380 | 1371 | if i < len(data)-1: |
1381 | 1372 | next_rev = data[i+1][1] |
1382 | 1373 | break |
1383 | | |
| 1374 | |
1384 | 1375 | return template(os.path.join("html", "notebook", "specific_revision.html"), |
1385 | 1376 | worksheet = ws, worksheet_filename = ws.filename(), |
1386 | 1377 | username = username, rev = rev, prev_rev = prev_rev, |
… |
… |
class Notebook(object): |
1393 | 1384 | Return the HTML for the "share" page of a worksheet. |
1394 | 1385 | |
1395 | 1386 | INPUT: |
1396 | | |
| 1387 | |
1397 | 1388 | - ``username`` - a string |
1398 | | |
| 1389 | |
1399 | 1390 | - ``worksheet`` - an instance of Worksheet |
1400 | 1391 | |
1401 | 1392 | OUTPUT: |
1402 | 1393 | |
1403 | 1394 | - string - the share page's HTML representation |
1404 | | |
| 1395 | |
1405 | 1396 | EXAMPLES:: |
1406 | | |
1407 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1397 | |
| 1398 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1408 | 1399 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1409 | 1400 | sage: nb.html_share(W, 'admin') |
1410 | 1401 | '...currently shared...add or remove collaborators...' |
… |
… |
class Notebook(object): |
1425 | 1416 | Return the HTML for the download or delete datafile page. |
1426 | 1417 | |
1427 | 1418 | INPUT: |
1428 | | |
| 1419 | |
1429 | 1420 | - ``username`` - a string |
1430 | | |
| 1421 | |
1431 | 1422 | - ``ws`` - an instance of Worksheet |
1432 | | |
| 1423 | |
1433 | 1424 | - ``filename`` - a string; the name of the file |
1434 | 1425 | |
1435 | 1426 | OUTPUT: |
1436 | | |
| 1427 | |
1437 | 1428 | - a string - the page rendered as HTML |
1438 | 1429 | |
1439 | 1430 | EXAMPLES:: |
1440 | | |
1441 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1431 | |
| 1432 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1442 | 1433 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1443 | 1434 | sage: nb.html_download_or_delete_datafile(W, 'admin', 'bar') |
1444 | 1435 | '...Data file: bar...DATA is a special variable...uploaded...' |
… |
… |
class Notebook(object): |
1448 | 1439 | worksheets = self.get_worksheets_with_viewer(username) |
1449 | 1440 | active_worksheets = [worksheet for worksheet in worksheets if worksheet.is_active(username)] |
1450 | 1441 | sort_worksheet_list(active_worksheets, 'name', False) |
1451 | | |
| 1442 | |
1452 | 1443 | ext = os.path.splitext(filename)[1].lower() |
1453 | 1444 | file_is_image, file_is_text = False, False |
1454 | 1445 | text_file_content = "" |
1455 | | |
| 1446 | |
1456 | 1447 | if ext in ['.png', '.jpg', '.gif']: |
1457 | 1448 | file_is_image = True |
1458 | 1449 | if ext in ['.txt', '.tex', '.sage', '.spyx', '.py', '.f', '.f90', '.c']: |
… |
… |
class Notebook(object): |
1471 | 1462 | |
1472 | 1463 | |
1473 | 1464 | ########################################################## |
1474 | | # Accessing all worksheets with certain properties. |
| 1465 | # Accessing all worksheets with certain properties. |
1475 | 1466 | ########################################################## |
1476 | 1467 | def get_all_worksheets(self): |
1477 | 1468 | return [x for x in self.__worksheets.itervalues() if not x.owner() in ['_sage_', 'pub']] |
1478 | | |
| 1469 | |
1479 | 1470 | def get_worksheets_with_collaborator(self, user): |
1480 | | if self.user_is_admin(user): return self.get_all_worksheets() |
| 1471 | if self.user_is_admin(user): return self.get_all_worksheets() |
1481 | 1472 | return [w for w in self.__worksheets.itervalues() if w.is_collaborator(user)] |
1482 | 1473 | |
1483 | 1474 | def get_worksheet_names_with_collaborator(self, user): |
… |
… |
class Notebook(object): |
1495 | 1486 | return [w for w in self.get_worksheets_with_owner(owner) if w.is_viewer(user)] |
1496 | 1487 | |
1497 | 1488 | def get_worksheet_names_with_viewer(self, user): |
1498 | | if self.user_is_admin(user): return [W.name() for W in self.get_all_worksheets()] |
| 1489 | if self.user_is_admin(user): return [W.name() for W in self.get_all_worksheets()] |
1499 | 1490 | return [W.name() for W in self.get_worksheets_with_viewer(user) if not W.docbrowser()] |
1500 | 1491 | |
1501 | 1492 | def get_worksheet_with_name(self, name): |
… |
… |
class Notebook(object): |
1508 | 1499 | """ |
1509 | 1500 | Get the worksheet with the given filename. If there is no |
1510 | 1501 | such worksheet, raise a ``KeyError``. |
1511 | | |
| 1502 | |
1512 | 1503 | INPUT: |
1513 | 1504 | |
1514 | 1505 | - ``filename`` - a string |
… |
… |
class Notebook(object): |
1557 | 1548 | Return the HTML for the debug window. |
1558 | 1549 | |
1559 | 1550 | OUTPUT: |
1560 | | |
| 1551 | |
1561 | 1552 | - a string - the debug window rendered as HTML |
1562 | | |
| 1553 | |
1563 | 1554 | EXAMPLES:: |
1564 | 1555 | |
1565 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1556 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1566 | 1557 | sage: nb.html_debug_window() |
1567 | 1558 | "\n<div class='debug_window'>...</div>" |
1568 | 1559 | """ |
1569 | 1560 | return template(os.path.join("html", "notebook", "debug_window.html")) |
1570 | 1561 | |
1571 | | |
| 1562 | |
1572 | 1563 | def html_plain_text_window(self, worksheet, username): |
1573 | 1564 | r""" |
1574 | 1565 | Return HTML for the window that displays a plain text version |
… |
… |
class Notebook(object): |
1585 | 1576 | - a string - the plain text window rendered as HTML |
1586 | 1577 | |
1587 | 1578 | EXAMPLES:: |
1588 | | |
1589 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1579 | |
| 1580 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1590 | 1581 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1591 | 1582 | sage: nb.html_plain_text_window(W, 'admin') |
1592 | 1583 | '...pre class="plaintext"...cell_intext...textfield...' |
1593 | 1584 | """ |
1594 | 1585 | plain_text = worksheet.plain_text(prompts=True, banner=False) |
1595 | 1586 | plain_text = escape(plain_text).strip() |
1596 | | |
| 1587 | |
1597 | 1588 | return template(os.path.join("html", "notebook", "plain_text_window.html"), |
1598 | 1589 | worksheet = worksheet, |
1599 | 1590 | worksheet_filename = worksheet.filename(), |
1600 | 1591 | username = username, plain_text = plain_text, |
1601 | 1592 | JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) |
1602 | | |
| 1593 | |
1603 | 1594 | def html_edit_window(self, worksheet, username): |
1604 | 1595 | r""" |
1605 | 1596 | Return HTML for a window for editing ``worksheet``. |
1606 | | |
| 1597 | |
1607 | 1598 | INPUT: |
1608 | | |
| 1599 | |
1609 | 1600 | - ``username`` - a string containing the username |
1610 | | |
| 1601 | |
1611 | 1602 | - ``worksheet`` - a Worksheet instance |
1612 | 1603 | |
1613 | 1604 | OUTPUT: |
1614 | | |
| 1605 | |
1615 | 1606 | - a string - the editing window's HTML representation |
1616 | 1607 | |
1617 | 1608 | EXAMPLES:: |
1618 | | |
1619 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1609 | |
| 1610 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1620 | 1611 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1621 | 1612 | sage: nb.html_edit_window(W, 'admin') |
1622 | 1613 | '...textarea class="plaintextedit"...{{{id=1|...//...}}}...' |
… |
… |
class Notebook(object): |
1624 | 1615 | text = worksheet.edit_text() |
1625 | 1616 | text = escape(text) |
1626 | 1617 | n_lines = text.count("\n")+1 |
1627 | | |
| 1618 | |
1628 | 1619 | return template(os.path.join("html", "notebook", "edit_window.html"), |
1629 | 1620 | worksheet = worksheet, |
1630 | 1621 | worksheet_filename = worksheet.filename(), |
1631 | 1622 | username = username, text = text, n_lines = n_lines, |
1632 | 1623 | JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) |
1633 | | |
| 1624 | |
1634 | 1625 | def html_beforepublish_window(self, worksheet, username): |
1635 | 1626 | r""" |
1636 | 1627 | Return HTML for the warning and decision page displayed prior |
1637 | 1628 | to publishing the given worksheet. |
1638 | | |
| 1629 | |
1639 | 1630 | INPUT: |
1640 | | |
| 1631 | |
1641 | 1632 | - ``worksheet`` - an instance of Worksheet |
1642 | | |
| 1633 | |
1643 | 1634 | - ``username`` - a string |
1644 | 1635 | |
1645 | 1636 | OUTPUT: |
… |
… |
class Notebook(object): |
1647 | 1638 | - a string - the pre-publication page rendered as HTML |
1648 | 1639 | |
1649 | 1640 | EXAMPLES:: |
1650 | | |
1651 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1641 | |
| 1642 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1652 | 1643 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1653 | 1644 | sage: nb.html_beforepublish_window(W, 'admin') |
1654 | 1645 | '...want to publish this worksheet?...re-publish when changes...' |
… |
… |
class Notebook(object): |
1668 | 1659 | worksheet_filename = worksheet.filename(), |
1669 | 1660 | username = username, |
1670 | 1661 | JSMATH = JSMATH, JEDITABLE_TINYMCE = JEDITABLE_TINYMCE) |
1671 | | |
| 1662 | |
1672 | 1663 | def html_afterpublish_window(self, worksheet, username, url, dtime): |
1673 | 1664 | r""" |
1674 | 1665 | Return HTML for a given worksheet's post-publication page. |
1675 | | |
| 1666 | |
1676 | 1667 | INPUT: |
1677 | | |
| 1668 | |
1678 | 1669 | - ``worksheet`` - an instance of Worksheet |
1679 | | |
| 1670 | |
1680 | 1671 | - ``username`` - a string |
1681 | | |
| 1672 | |
1682 | 1673 | - ``url`` - a string representing the URL of the published |
1683 | 1674 | worksheet |
1684 | | |
| 1675 | |
1685 | 1676 | - ``dtime`` - an instance of time.struct_time representing the |
1686 | 1677 | publishing time |
1687 | 1678 | |
1688 | 1679 | OUTPUT: |
1689 | 1680 | |
1690 | 1681 | - a string - the post-publication page rendered as HTML |
1691 | | |
1692 | 1682 | """ |
1693 | 1683 | from time import strftime |
1694 | 1684 | time = strftime("%B %d, %Y %I:%M %p", dtime) |
1695 | | |
| 1685 | |
1696 | 1686 | return template(os.path.join("html", "notebook", "afterpublish_window.html"), |
1697 | 1687 | worksheet = worksheet, |
1698 | 1688 | worksheet_filename = worksheet.filename(), |
… |
… |
class Notebook(object): |
1704 | 1694 | Return HTML for the "Upload Data" window. |
1705 | 1695 | |
1706 | 1696 | INPUT: |
1707 | | |
| 1697 | |
1708 | 1698 | - ``worksheet`` - an instance of Worksheet |
1709 | | |
| 1699 | |
1710 | 1700 | - ``username`` - a string |
1711 | 1701 | |
1712 | 1702 | OUTPUT: |
… |
… |
class Notebook(object): |
1714 | 1704 | - a string - the HTML representation of the data upload window |
1715 | 1705 | |
1716 | 1706 | EXAMPLES:: |
1717 | | |
1718 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1707 | |
| 1708 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1719 | 1709 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1720 | 1710 | sage: nb.html_upload_data_window(W, 'admin') |
1721 | 1711 | '...Upload or Create Data File...Browse...url...name of a new...' |
… |
… |
class Notebook(object): |
1730 | 1720 | Return the HTML for a worksheet's index page. |
1731 | 1721 | |
1732 | 1722 | INPUT: |
1733 | | |
| 1723 | |
1734 | 1724 | - ``worksheet_filename`` - a string (default: None) |
1735 | | |
| 1725 | |
1736 | 1726 | - ``username`` - a string (default: 'guest') |
1737 | 1727 | |
1738 | 1728 | - ``show_debug`` - a bool (default: False) |
1739 | | |
| 1729 | |
1740 | 1730 | - ``admin`` - a bool (default: False) |
1741 | 1731 | |
1742 | 1732 | OUTPUT: |
… |
… |
class Notebook(object): |
1744 | 1734 | - a string - the worksheet rendered as HTML |
1745 | 1735 | |
1746 | 1736 | EXAMPLES:: |
1747 | | |
1748 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1737 | |
| 1738 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1749 | 1739 | sage: W = nb.create_new_worksheet('Test', 'admin') |
1750 | 1740 | sage: nb.html(W.filename(), 'admin') |
1751 | 1741 | '...Test...cell_input...plainclick...state_number...worksheet_locked=false...' |
… |
… |
class Notebook(object): |
1762 | 1752 | template_page = os.path.join("html", "notebook", "index.html") |
1763 | 1753 | if W.docbrowser(): |
1764 | 1754 | template_page = os.path.join("html", "notebook", "doc_page.html") |
1765 | | |
| 1755 | |
1766 | 1756 | return template(template_page, worksheet = W, |
1767 | 1757 | worksheet_filename = W.filename(), |
1768 | 1758 | worksheet_html = W.html(), notebook = self, |
… |
… |
def load_notebook(dir, interface=None, p |
1775 | 1765 | """ |
1776 | 1766 | Load and return a notebook from a given directory. Create a new |
1777 | 1767 | one in that directory, if one isn't already there. |
1778 | | |
| 1768 | |
1779 | 1769 | INPUT: |
1780 | | |
| 1770 | |
1781 | 1771 | - ``dir`` - a string that defines a directory name |
1782 | | |
| 1772 | |
1783 | 1773 | - ``interface`` - the address of the interface the server listens at |
1784 | | |
| 1774 | |
1785 | 1775 | - ``port`` - the port the server listens on |
1786 | | |
| 1776 | |
1787 | 1777 | - ``secure`` - whether the notebook is secure |
1788 | 1778 | |
1789 | 1779 | OUTPUT: |
… |
… |
def load_notebook(dir, interface=None, p |
1798 | 1788 | raise KeyboardInterrupt, "Interrupted notebook migration. Delete the directory '%s' and try again."%(os.path.abspath(dir+'.sagenb')) |
1799 | 1789 | return nb |
1800 | 1790 | dir += '.sagenb' |
1801 | | |
| 1791 | |
1802 | 1792 | dir = make_path_relative(dir) |
1803 | 1793 | nb = Notebook(dir) |
1804 | 1794 | nb.interface = interface |
… |
… |
def migrate_old_notebook_v1(dir): |
1820 | 1810 | """ |
1821 | 1811 | nb_sobj = os.path.join(dir, 'nb.sobj') |
1822 | 1812 | old_nb = cPickle.loads(open(nb_sobj).read()) |
1823 | | |
| 1813 | |
1824 | 1814 | ###################################################################### |
1825 | 1815 | # Tell user what is going on and make a backup |
1826 | 1816 | ###################################################################### |
1827 | 1817 | |
1828 | 1818 | print "" |
1829 | 1819 | print "*"*80 |
1830 | | print "*" |
| 1820 | print "*" |
1831 | 1821 | print "* The Sage notebook at" |
1832 | | print "*" |
| 1822 | print "*" |
1833 | 1823 | print "* '%s'"%os.path.abspath(dir) |
1834 | | print "*" |
| 1824 | print "*" |
1835 | 1825 | print "* will be upgraded to a new format and stored in" |
1836 | 1826 | print "*" |
1837 | 1827 | print "* '%s.sagenb'."%os.path.abspath(dir) |
… |
… |
def migrate_old_notebook_v1(dir): |
1847 | 1837 | # Create new notebook |
1848 | 1838 | new_nb = Notebook(dir+'.sagenb') |
1849 | 1839 | |
1850 | | # Define a function for transfering the attributes of one object to another. |
| 1840 | # Define a function for transfering the attributes of one object to another. |
1851 | 1841 | def transfer_attributes(old, new, attributes): |
1852 | 1842 | for attr_old, attr_new in attributes: |
1853 | 1843 | if hasattr(old, attr_old): |
1854 | 1844 | setattr(new, attr_new, getattr(old, attr_old)) |
1855 | | |
| 1845 | |
1856 | 1846 | # Transfer all the notebook attributes to our new notebook object |
1857 | | |
| 1847 | |
1858 | 1848 | new_nb.conf().confs = old_nb.conf().confs |
1859 | 1849 | for t in ['pretty_print', 'server_pool', 'ulimit', 'system']: |
1860 | 1850 | if hasattr(old_nb, '_Notebook__' + t): |
1861 | 1851 | new_nb.conf().confs[t] = getattr(old_nb, '_Notebook__' + t) |
1862 | | |
| 1852 | |
1863 | 1853 | # Now update the user data from the old notebook to the new one: |
1864 | 1854 | print "Migrating %s user accounts..."%len(old_nb.users()) |
1865 | 1855 | users = new_nb.users() |
… |
… |
def migrate_old_notebook_v1(dir): |
1874 | 1864 | # Fix the __conf field, which is also an instance of a class |
1875 | 1865 | new_user.conf().confs = old_user.conf().confs |
1876 | 1866 | users[new_user.username()] = new_user |
1877 | | |
| 1867 | |
1878 | 1868 | ###################################################################### |
1879 | 1869 | # Set the worksheets of the new notebook equal to the ones from |
1880 | 1870 | # the old one. |
1881 | 1871 | ###################################################################### |
1882 | | |
| 1872 | |
1883 | 1873 | ###################################################################### |
1884 | | |
| 1874 | |
1885 | 1875 | def migrate_old_worksheet(old_worksheet): |
1886 | 1876 | """ |
1887 | | Migrates an old worksheet to the new format. |
| 1877 | Migrates an old worksheet to the new format. |
1888 | 1878 | """ |
1889 | 1879 | old_ws_dirname = old_ws._Worksheet__filename.partition(os.path.sep)[-1] |
1890 | 1880 | new_ws = new_nb.worksheet(old_ws.owner(), old_ws_dirname) |
… |
… |
def migrate_old_notebook_v1(dir): |
1896 | 1886 | if isinstance(user,str): |
1897 | 1887 | # There was a bug in the old notebook where sometimes the |
1898 | 1888 | # user was the *module* "user", so we don't include that |
1899 | | # invalid data. |
| 1889 | # invalid data. |
1900 | 1890 | tags[user] = [val] |
1901 | 1891 | except AttributeError: |
1902 | 1892 | pass |
… |
… |
def migrate_old_notebook_v1(dir): |
1909 | 1899 | |
1910 | 1900 | ws_pub = old_ws.worksheet_that_was_published().filename().split('/') |
1911 | 1901 | ws_pub = (ws_pub[0],int(ws_pub[1])) |
1912 | | |
| 1902 | |
1913 | 1903 | obj = {'name':old_ws.name(), 'system':old_ws.system(), |
1914 | 1904 | 'viewers':old_ws.viewers(), 'collaborators':old_ws.collaborators(), |
1915 | 1905 | 'pretty_print':old_ws.pretty_print(), 'ratings':old_ws.ratings(), |
… |
… |
def migrate_old_notebook_v1(dir): |
1920 | 1910 | } |
1921 | 1911 | |
1922 | 1912 | new_ws.reconstruct_from_basic(obj) |
1923 | | |
| 1913 | |
1924 | 1914 | base = os.path.join(dir, 'worksheets', old_ws.filename()) |
1925 | 1915 | worksheet_file = os.path.join(base, 'worksheet.txt') |
1926 | 1916 | if os.path.exists(worksheet_file): |
… |
… |
def migrate_old_notebook_v1(dir): |
1946 | 1936 | shutil.copytree(old_ws.cells_directory(), dest) |
1947 | 1937 | except Exception, msg: |
1948 | 1938 | print msg |
1949 | | |
| 1939 | |
1950 | 1940 | |
1951 | 1941 | return new_ws |
1952 | | |
| 1942 | |
1953 | 1943 | worksheets = {} |
1954 | 1944 | num_worksheets = len(old_nb._Notebook__worksheets) |
1955 | 1945 | print "Migrating (at most) %s worksheets..."%num_worksheets |
… |
… |
def make_path_relative(dir): |
1989 | 1979 | |
1990 | 1980 | INPUT: |
1991 | 1981 | |
1992 | | - ``dir`` - a string containing, e.g., a directory name |
| 1982 | - ``dir`` - a string containing, e.g., a directory name |
1993 | 1983 | |
1994 | 1984 | OUTPUT: |
1995 | 1985 | |
… |
… |
def make_path_relative(dir): |
2002 | 1992 | |
2003 | 1993 | ########################################################## |
2004 | 1994 | # Misc |
2005 | | ########################################################## |
| 1995 | ########################################################## |
2006 | 1996 | |
2007 | 1997 | def clean_name(name): |
2008 | 1998 | return ''.join([x if (x.isalnum() or x == '_') else '_' for x in name]) |
… |
… |
def sort_worksheet_list(v, sort, reverse |
2012 | 2002 | Sort a given list on a given key, in a given order. |
2013 | 2003 | |
2014 | 2004 | INPUT: |
2015 | | |
| 2005 | |
2016 | 2006 | - ``sort`` - a string; 'last_edited', 'owner', 'rating', or 'name' |
2017 | | |
| 2007 | |
2018 | 2008 | - ``reverse`` - a bool; if True, reverse the order of the sort. |
2019 | 2009 | |
2020 | 2010 | OUTPUT: |
diff --git a/sagenb/notebook/notebook_object.py b/sagenb/notebook/notebook_object.py
a
|
b
|
def test_notebook(admin_passwd, secure=F |
207 | 207 | sage: passwd = str(randint(1,1<<128)) |
208 | 208 | sage: nb = test_notebook(passwd, address='localhost', port=8060) |
209 | 209 | sage: import urllib |
210 | | sage: h = urllib.urlopen('https://localhost:8060') |
| 210 | sage: h = urllib.urlopen('http://localhost:8060') |
211 | 211 | sage: homepage = h.read() |
212 | 212 | sage: h.close() |
213 | 213 | sage: 'html' in homepage |
… |
… |
def test_notebook(admin_passwd, secure=F |
238 | 238 | except pexpect.EOF: |
239 | 239 | pass |
240 | 240 | p.close(force=True) |
241 | | shutil.rmtree(nb.directory()) |
| 241 | shutil.rmtree(nb._dir) |
242 | 242 | p.dispose = dispose |
243 | 243 | if verbose: |
244 | 244 | print "Notebook started." |
diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
a
|
b
|
TESTS: |
6 | 6 | It is important that this file never be imported by default on |
7 | 7 | startup by Sage, since it is very expensive, since importing Twisted |
8 | 8 | is expensive. This doctest verifies that twist.py isn't imported on |
9 | | startup. |
10 | | |
11 | | :: |
| 9 | startup.:: |
12 | 10 | |
13 | 11 | sage: os.system("sage -startuptime | grep twisted\.web2 1>/dev/null") != 0 # !=0 means not found |
14 | 12 | True |
… |
… |
class Worksheet_do_upload_data(Worksheet |
594 | 592 | if not os.path.isfile(dest): |
595 | 593 | return HTMLResponse(stream=message('Suspicious filename "%s" encountered uploading file.%s' % (name, backlinks), worksheet_url)) |
596 | 594 | os.unlink(dest) |
597 | | |
| 595 | |
598 | 596 | response = http.RedirectResponse(worksheet_url + '/datafile?name=%s' % name) |
599 | 597 | |
600 | 598 | if url != '': |
… |
… |
class SettingsPage(resource.PostableReso |
956 | 954 | autosave = int(request.args.get('autosave', [0])[0]) * 60 |
957 | 955 | if autosave: |
958 | 956 | nu['autosave_interval'] = autosave |
959 | | redirect_to_home = True |
| 957 | redirect_to_home = True |
960 | 958 | |
961 | 959 | old = request.args.get('old-pass', [None])[0] |
962 | 960 | new = request.args.get('new-pass', [None])[0] |
… |
… |
def render_worksheet_list(args, pub, use |
1526 | 1524 | - ``username`` - the user whose worksheets we are |
1527 | 1525 | listing |
1528 | 1526 | |
1529 | | OUTPUT: |
| 1527 | OUTPUT: |
1530 | 1528 | |
1531 | 1529 | a string |
1532 | 1530 | """ |
… |
… |
class EmptyTrash(resource.Resource): |
1602 | 1600 | This twisted resource empties the trash of the current user when it |
1603 | 1601 | is rendered. |
1604 | 1602 | |
1605 | | EXAMPLES: |
1606 | | |
1607 | | We create an instance of this resource. |
1608 | | |
1609 | | :: |
| 1603 | EXAMPLES: |
| 1604 | |
| 1605 | We create an instance of this resource.:: |
1610 | 1606 | |
1611 | 1607 | sage: import sagenb.notebook.twist |
1612 | 1608 | sage: E = sagenb.notebook.twist.EmptyTrash('sage'); E |
… |
… |
class EmptyTrash(resource.Resource): |
1619 | 1615 | Rendering this resource (1) empties the trash, and (2) returns a |
1620 | 1616 | message. |
1621 | 1617 | |
1622 | | EXAMPLES: |
| 1618 | EXAMPLES: |
1623 | 1619 | |
1624 | 1620 | We create a notebook with a worksheet, put it in the |
1625 | 1621 | trash, then empty the trash by creating and rendering this |
1626 | | worksheet. |
1627 | | |
1628 | | :: |
1629 | | |
1630 | | sage: n = sagenb.notebook.notebook.Notebook('notebook-test') |
| 1622 | worksheet.:: |
| 1623 | |
| 1624 | sage: n = sagenb.notebook.notebook.load_notebook('notebook-test.sagenb') |
1631 | 1625 | sage: n.add_user('sage','sage','sage@sagemath.org',force=True) |
1632 | 1626 | sage: W = n.new_worksheet_with_title_from_text('Sage', owner='sage') |
1633 | 1627 | sage: W.move_to_trash('sage') |
… |
… |
class EmptyTrash(resource.Resource): |
1636 | 1630 | sage: sagenb.notebook.twist.notebook = n |
1637 | 1631 | sage: E = sagenb.notebook.twist.EmptyTrash('sage'); E |
1638 | 1632 | <sagenb.notebook.twist.EmptyTrash object at ...> |
1639 | | sage: E.render(None) |
1640 | | <twisted.web2.http.Response code=200, streamlen=...> |
| 1633 | sage: from sagenb.notebook.twist import HTMLResponse |
| 1634 | sage: ctx = HTMLResponse(stream = 'foo') |
| 1635 | sage: ctx.headers.addRawHeader('referer', 'over there') |
| 1636 | sage: E.render(ctx) |
| 1637 | <RedirectResponse 301 Document moved to over there.> |
1641 | 1638 | |
1642 | 1639 | Finally we verify that the trashed worksheet is gone:: |
1643 | 1640 | |
… |
… |
class JSMath_js(resource.Resource): |
1844 | 1841 | s = template(os.path.join('js', 'jsmath.js'), |
1845 | 1842 | jsmath_macros = jsmath_macros, |
1846 | 1843 | jsmath_image_fonts = jsmath_image_fonts) |
1847 | | |
| 1844 | |
1848 | 1845 | return http.Response(stream=s) |
1849 | 1846 | |
1850 | 1847 | class Main_js(resource.Resource): |
… |
… |
import re |
1963 | 1960 | re_valid_username = re.compile('[a-z|A-Z|0-9|_|.|@]*') |
1964 | 1961 | def is_valid_username(username): |
1965 | 1962 | r""" |
1966 | | Returns True if and only if ``username`` is valid, i.e., between 3 |
1967 | | and 64 characters long. You may only use letters, numbers, |
1968 | | underscores, @, and dots. |
| 1963 | Returns whether a candidate username is valid. It must contain |
| 1964 | between 3 and 65 of these characters: letters, numbers, |
| 1965 | underscores, @, and/or dots ('.'). |
| 1966 | |
| 1967 | INPUT: |
| 1968 | |
| 1969 | - ``username`` - a string; the candidate username |
| 1970 | |
| 1971 | OUTPUT: |
| 1972 | |
| 1973 | - a boolean |
1969 | 1974 | |
1970 | 1975 | EXAMPLES:: |
1971 | 1976 | |
1972 | 1977 | sage: from sagenb.notebook.twist import is_valid_username |
1973 | | |
1974 | | ``username`` must start with a letter |
1975 | | |
1976 | | :: |
1977 | | |
1978 | 1978 | sage: is_valid_username('mark10') |
1979 | 1979 | True |
1980 | 1980 | sage: is_valid_username('10mark') |
1981 | 1981 | False |
1982 | | |
1983 | | ``username`` must be between 4 and 32 characters long |
1984 | | |
1985 | | :: |
1986 | | |
1987 | | sage: is_valid_username('bob') |
| 1982 | sage: is_valid_username('me') |
1988 | 1983 | False |
1989 | | sage: is_valid_username('I_love_computer_science_and_maths') #33 characters long |
| 1984 | sage: is_valid_username('abcde' * 13) |
1990 | 1985 | False |
1991 | | |
1992 | | ``username`` must not have more than one dot (.) |
1993 | | |
1994 | | :: |
1995 | | |
1996 | | sage: is_valid_username('david.andrews') |
1997 | | True |
1998 | | sage: is_valid_username('david.m.andrews') |
1999 | | False |
2000 | | sage: is_valid_username('math125.TA.5') |
2001 | | False |
2002 | | |
2003 | | ``username`` must not have any spaces |
2004 | | |
2005 | | :: |
2006 | | |
2007 | 1986 | sage: is_valid_username('David Andrews') |
2008 | 1987 | False |
2009 | 1988 | sage: is_valid_username('David M. Andrews') |
2010 | 1989 | False |
2011 | | |
2012 | | :: |
2013 | | |
2014 | 1990 | sage: is_valid_username('sarah_andrews') |
2015 | 1991 | True |
2016 | | |
2017 | | :: |
2018 | | |
2019 | 1992 | sage: is_valid_username('TA-1') |
2020 | 1993 | False |
2021 | 1994 | sage: is_valid_username('math125-TA') |
2022 | 1995 | False |
2023 | | |
2024 | | :: |
2025 | | |
2026 | 1996 | sage: is_valid_username('dandrews@sagemath.org') |
2027 | | False |
| 1997 | True |
2028 | 1998 | """ |
2029 | 1999 | import string |
2030 | 2000 | |
… |
… |
class LoginResourceClass(resource.Resour |
2413 | 2383 | template_dict = {'accounts': notebook.get_accounts(), |
2414 | 2384 | 'default_user': notebook.default_user(), |
2415 | 2385 | 'recovery': notebook.conf()['email'], |
2416 | | 'sage_version':SAGE_VERSION} |
| 2386 | 'sage_version':SAGE_VERSION} |
2417 | 2387 | return HTMLResponse(stream=template(os.path.join('html', 'login.html'), **template_dict)) |
2418 | 2388 | |
2419 | 2389 | def childFactory(self, request, name): |
… |
… |
class FailedToplevel(Toplevel): |
2478 | 2448 | 'default_user': self.username, |
2479 | 2449 | 'password_error': True, |
2480 | 2450 | 'recovery': notebook.conf()['email'], |
2481 | | 'sage_version':SAGE_VERSION} |
| 2451 | 'sage_version':SAGE_VERSION} |
2482 | 2452 | return HTMLResponse(stream=template(os.path.join('html', 'login.html'), **template_dict)) |
2483 | 2453 | elif self.problem == 'suspended': |
2484 | 2454 | return HTMLResponse(stream = message("Your account is currently suspended.")) |
diff --git a/sagenb/notebook/worksheet.py b/sagenb/notebook/worksheet.py
a
|
b
|
from sagenb.interfaces import (Worksheet |
39 | 39 | WorksheetProcess_ReferenceImplementation, |
40 | 40 | WorksheetProcess_RemoteExpectImplementation) |
41 | 41 | |
42 | | |
| 42 | |
43 | 43 | import sagenb.misc.support as support |
44 | 44 | |
45 | 45 | # Imports specifically relevant to the sage notebook |
… |
… |
SAGE_END = SC + 'e' |
72 | 72 | SAGE_ERROR = SC + 'r' |
73 | 73 | |
74 | 74 | # Integers that define which folder this worksheet is in |
75 | | # relative to a given user. |
| 75 | # relative to a given user. |
76 | 76 | ARCHIVED = 0 |
77 | 77 | ACTIVE = 1 |
78 | 78 | TRASH = 2 |
… |
… |
all_worksheet_processes = [] |
81 | 81 | |
82 | 82 | def update_worksheets(): |
83 | 83 | """ |
84 | | Iterate through and "update" all the worksheets. This is needed for things like |
85 | | wall timeouts. |
| 84 | Iterate through and "update" all the worksheets. This is needed |
| 85 | for things like wall timeouts. |
86 | 86 | """ |
87 | 87 | for S in all_worksheet_processes: |
88 | 88 | S.update() |
… |
… |
def worksheet_filename(name, owner): |
93 | 93 | """ |
94 | 94 | Return the relative directory name of this worksheet with given |
95 | 95 | name and owner. |
96 | | |
| 96 | |
97 | 97 | INPUT: |
98 | | |
99 | | |
| 98 | |
100 | 99 | - ``name`` - string, which may have spaces and funny |
101 | 100 | characters, which are replaced by underscores. |
102 | | |
| 101 | |
103 | 102 | - ``owner`` - string, with no spaces or funny |
104 | 103 | characters |
105 | | |
106 | | |
| 104 | |
107 | 105 | OUTPUT: string |
108 | | |
| 106 | |
109 | 107 | EXAMPLES:: |
110 | | |
| 108 | |
111 | 109 | sage: sagenb.notebook.worksheet.worksheet_filename('Example worksheet 3', 'sage10') |
112 | 110 | 'sage10/Example_worksheet_3' |
113 | 111 | sage: sagenb.notebook.worksheet.worksheet_filename('Example#%&! work\\sheet 3', 'sage10') |
… |
… |
def Worksheet_from_basic(obj, notebook_w |
127 | 125 | stores worksheets, i.e., nb.worksheet_directory(). |
128 | 126 | |
129 | 127 | OUTPUT: |
130 | | |
| 128 | |
131 | 129 | - a worksheet |
132 | 130 | |
133 | 131 | EXAMPLES:: |
| 132 | |
134 | 133 | sage: import sagenb.notebook.worksheet |
135 | | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, '', system='gap', owner='sageuser', pretty_print=True, auto_publish=True) |
| 134 | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, tmp_dir(), system='gap', owner='sageuser', pretty_print=True, auto_publish=True) |
136 | 135 | sage: _=W.new_cell_after(0); B = W.basic() |
137 | | sage: W0 = sagenb.notebook.worksheet.Worksheet_from_basic(B, '') |
| 136 | sage: W0 = sagenb.notebook.worksheet.Worksheet_from_basic(B, tmp_dir()) |
138 | 137 | sage: W0.basic() == B |
139 | 138 | True |
140 | 139 | """ |
… |
… |
class Worksheet(object): |
150 | 149 | auto_publish=False, create_directories=True): |
151 | 150 | """ |
152 | 151 | Create and initialize a new worksheet. |
153 | | |
| 152 | |
154 | 153 | INPUT: |
155 | | |
156 | | |
| 154 | |
157 | 155 | - ``name`` - string; the name of this worksheet |
158 | | |
| 156 | |
159 | 157 | - ``id_number`` - Integer; name of the directory in which the |
160 | 158 | worksheet's data is stored |
161 | | |
| 159 | |
162 | 160 | - ``notebook_worksheet_directory`` - string; the |
163 | 161 | directory in which the notebook object that contains this worksheet |
164 | 162 | stores worksheets, i.e., nb.worksheet_directory(). |
165 | | |
| 163 | |
166 | 164 | - ``system`` - string; 'sage', 'gp', 'singular', etc. |
167 | 165 | - the math software system in which all code is evaluated by |
168 | 166 | default |
169 | | |
| 167 | |
170 | 168 | - ``owner`` - string; username of the owner of this |
171 | 169 | worksheet |
172 | | |
| 170 | |
173 | 171 | - ``docbrowser`` - bool (default: False); whether this |
174 | 172 | is a docbrowser worksheet |
175 | | |
| 173 | |
176 | 174 | - ``pretty_print`` - bool (default: False); whether |
177 | 175 | all output is pretty printed by default. |
178 | 176 | |
… |
… |
class Worksheet(object): |
180 | 178 | creates various files and directories where data will be |
181 | 179 | stored. This option is here only for the |
182 | 180 | migrate_old_notebook method in notebook.py |
183 | | |
184 | | |
| 181 | |
185 | 182 | EXAMPLES: We test the constructor via an indirect doctest:: |
186 | | |
187 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 183 | |
| 184 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
188 | 185 | sage: W = nb.create_new_worksheet('Test', 'admin') |
189 | 186 | sage: W |
190 | | admin/0: [Cell 0; in=, out=] |
| 187 | admin/0: [Cell 1; in=, out=] |
191 | 188 | """ |
192 | 189 | if name is None: |
193 | 190 | # A fresh worksheet |
… |
… |
class Worksheet(object): |
205 | 202 | |
206 | 203 | # state sequence number, used for sync |
207 | 204 | self.__state_number = 0 |
208 | | |
| 205 | |
209 | 206 | # Initialize the cell id counter. |
210 | 207 | self.__next_id = 0 |
211 | 208 | |
… |
… |
class Worksheet(object): |
250 | 247 | EXAMPLES:: |
251 | 248 | |
252 | 249 | sage: from sagenb.notebook.worksheet import Worksheet |
253 | | sage: W = Worksheet('test', 2, '', owner='sageuser') |
| 250 | sage: W = Worksheet('test', 2, tmp_dir(), owner='sageuser') |
254 | 251 | sage: W.id_number() |
255 | 252 | 2 |
256 | 253 | sage: type(W.id_number()) |
… |
… |
class Worksheet(object): |
268 | 265 | configuration of this worksheet, except the actual cells and |
269 | 266 | the data files in the DATA directory and images and other data |
270 | 267 | in the individual cell directories. |
271 | | |
| 268 | |
272 | 269 | EXAMPLES:: |
273 | 270 | |
274 | 271 | sage: import sagenb.notebook.worksheet |
275 | | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, '', owner='sage') |
| 272 | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, tmp_dir(), owner='sage') |
276 | 273 | sage: sorted((W.basic().items())) |
277 | 274 | [('auto_publish', False), |
278 | | ('collaborators', []), |
279 | | ('id_number', 0), |
280 | | ('last_change', ('sage', ...)), |
281 | | ('name', 'test'), |
282 | | ('owner', 'sage'), |
283 | | ('pretty_print', False), |
284 | | ('published_id_number', None), |
285 | | ('ratings', []), |
286 | | ('system', None), |
287 | | ('tags', {}), |
288 | | ('viewers', []), |
289 | | ('worksheet_that_was_published', None)] |
| 275 | ('collaborators', []), |
| 276 | ('id_number', 0), |
| 277 | ('last_change', ('sage', ...)), |
| 278 | ('name', 'test'), |
| 279 | ('owner', 'sage'), |
| 280 | ('pretty_print', False), |
| 281 | ('published_id_number', None), |
| 282 | ('ratings', []), |
| 283 | ('system', None), |
| 284 | ('tags', {'sage': [1]}), |
| 285 | ('viewers', []), |
| 286 | ('worksheet_that_was_published', ('sage', 0))] |
290 | 287 | """ |
291 | 288 | try: |
292 | 289 | published_id_number = int(os.path.split(self.__published_version)[1]) |
… |
… |
class Worksheet(object): |
297 | 294 | ws_pub = self.__worksheet_came_from |
298 | 295 | except AttributeError: |
299 | 296 | ws_pub = (self.owner(), self.id_number()) |
300 | | |
| 297 | |
301 | 298 | d = {############# |
302 | 299 | # basic identification |
303 | 300 | 'name':self.name(), |
… |
… |
class Worksheet(object): |
324 | 321 | # worksheet. Otherwise ws_pub is None. |
325 | 322 | 'worksheet_that_was_published':ws_pub, |
326 | 323 | # Whether or not this worksheet should automatically be |
327 | | # republished when changed. |
| 324 | # republished when changed. |
328 | 325 | 'auto_publish':self.is_auto_publish(), |
329 | 326 | |
330 | 327 | # Appearance: e.g., whether to pretty print this |
… |
… |
class Worksheet(object): |
365 | 362 | ``id_number`` is a key of obj; otherwise not. |
366 | 363 | |
367 | 364 | EXAMPLES:: |
| 365 | |
368 | 366 | sage: import sagenb.notebook.worksheet |
369 | | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, '', system='gap', owner='sageuser', pretty_print=True, auto_publish=True) |
| 367 | sage: W = sagenb.notebook.worksheet.Worksheet('test', 0, tmp_dir(), system='gap', owner='sageuser', pretty_print=True, auto_publish=True) |
370 | 368 | sage: W.new_cell_after(0) |
371 | 369 | Cell 1; in=, out= |
372 | 370 | sage: b = W.basic() |
373 | 371 | sage: W0 = sagenb.notebook.worksheet.Worksheet() |
374 | | sage: W0.reconstruct_from_basic(b,'') |
| 372 | sage: W0.reconstruct_from_basic(b, tmp_dir()) |
375 | 373 | sage: W0.basic() == W.basic() |
376 | 374 | True |
377 | 375 | """ |
… |
… |
class Worksheet(object): |
407 | 405 | def __cmp__(self, other): |
408 | 406 | """ |
409 | 407 | We compare two worksheets. |
410 | | |
| 408 | |
411 | 409 | INPUT: |
412 | | |
413 | | |
| 410 | |
414 | 411 | - ``self, other`` - worksheets |
415 | | |
416 | | |
| 412 | |
417 | 413 | OUTPUT: |
418 | | |
419 | | |
| 414 | |
420 | 415 | - ``-1,0,1`` - comparison is on the underlying |
421 | 416 | file names. |
422 | | |
423 | | |
| 417 | |
424 | 418 | EXAMPLES:: |
425 | | |
426 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 419 | |
| 420 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
427 | 421 | sage: W2 = nb.create_new_worksheet('test2', 'admin') |
428 | 422 | sage: W1 = nb.create_new_worksheet('test1', 'admin') |
429 | 423 | sage: cmp(W1, W2) |
… |
… |
class Worksheet(object): |
440 | 434 | r""" |
441 | 435 | Return string representation of this worksheet, which is simply the |
442 | 436 | string representation of the underlying list of cells. |
443 | | |
| 437 | |
444 | 438 | OUTPUT: string |
445 | | |
| 439 | |
446 | 440 | EXAMPLES:: |
447 | | |
448 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 441 | |
| 442 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
449 | 443 | sage: W = nb.create_new_worksheet('test1', 'admin') |
450 | 444 | sage: W.__repr__() |
451 | | 'admin/0: [Cell 0; in=, out=]' |
452 | | sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}') |
| 445 | 'admin/0: [Cell 1; in=, out=]' |
| 446 | sage: W.edit_save('{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}') |
453 | 447 | sage: W.__repr__() |
454 | 448 | 'admin/0: [Cell 0; in=2+3, out=\n5, Cell 10; in=2+8, out=\n10]' |
455 | 449 | """ |
… |
… |
class Worksheet(object): |
457 | 451 | def __len__(self): |
458 | 452 | r""" |
459 | 453 | Return the number of cells in this worksheet. |
460 | | |
| 454 | |
461 | 455 | OUTPUT: int |
462 | | |
| 456 | |
463 | 457 | EXAMPLES:: |
464 | | |
465 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 458 | |
| 459 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
466 | 460 | sage: W = nb.create_new_worksheet('test1', 'admin') |
467 | 461 | sage: len(W) |
468 | 462 | 1 |
469 | | sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}') |
| 463 | sage: W.edit_save('{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}') |
470 | 464 | sage: len(W) |
471 | 465 | 2 |
472 | 466 | """ |
… |
… |
class Worksheet(object): |
488 | 482 | def docbrowser(self): |
489 | 483 | """ |
490 | 484 | Return True if this is a docbrowser worksheet. |
491 | | |
| 485 | |
492 | 486 | OUTPUT: bool |
493 | | |
| 487 | |
494 | 488 | EXAMPLES: We first create a standard worksheet for which docbrowser |
495 | 489 | is of course False:: |
496 | | |
497 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 490 | |
| 491 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
498 | 492 | sage: W = nb.create_new_worksheet('test1', 'admin') |
499 | 493 | sage: W.docbrowser() |
500 | 494 | False |
501 | | |
| 495 | |
502 | 496 | We create a worksheet for which docbrowser is True:: |
503 | | |
| 497 | |
504 | 498 | sage: W = nb.create_new_worksheet('docs', 'admin', docbrowser=True) |
505 | 499 | sage: W.docbrowser() |
506 | 500 | True |
… |
… |
class Worksheet(object): |
520 | 514 | """ |
521 | 515 | Return a (reference to the) list of the collaborators who can also |
522 | 516 | view and modify this worksheet. |
523 | | |
| 517 | |
524 | 518 | OUTPUT: list |
525 | | |
| 519 | |
526 | 520 | EXAMPLES:: |
527 | | |
528 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 521 | |
| 522 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
529 | 523 | sage: W = nb.create_new_worksheet('test1', 'admin') |
530 | 524 | sage: C = W.collaborators(); C |
531 | 525 | [] |
… |
… |
class Worksheet(object): |
538 | 532 | except AttributeError: |
539 | 533 | self.__collaborators = [] |
540 | 534 | return self.__collaborators |
541 | | |
| 535 | |
542 | 536 | def collaborator_names(self, max=None): |
543 | 537 | """ |
544 | 538 | Returns a string of the non-owner collaborators on this worksheet. |
545 | | |
| 539 | |
546 | 540 | INPUT: |
547 | | |
548 | | |
| 541 | |
549 | 542 | - ``max`` - an integer. If this is specified, then |
550 | 543 | only max number of collaborators are shown. |
551 | | |
552 | | |
| 544 | |
553 | 545 | EXAMPLES:: |
554 | | |
555 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 546 | |
| 547 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
556 | 548 | sage: W = nb.create_new_worksheet('test1', 'admin') |
557 | 549 | sage: C = W.collaborators(); C |
558 | 550 | [] |
… |
… |
class Worksheet(object): |
572 | 564 | """ |
573 | 565 | Set the list of collaborators to those listed in the list v of |
574 | 566 | strings. |
575 | | |
| 567 | |
576 | 568 | INPUT: |
577 | | |
578 | | |
| 569 | |
579 | 570 | - ``v`` - a list of strings |
580 | | |
581 | | |
| 571 | |
582 | 572 | EXAMPLES:: |
583 | | |
584 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 573 | |
| 574 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
585 | 575 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
586 | 576 | sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True) |
587 | 577 | sage: W = nb.create_new_worksheet('test1', 'admin') |
588 | 578 | sage: W.set_collaborators(['sage', 'admin', 'hilbert', 'sage']) |
589 | | |
| 579 | |
590 | 580 | Note that repeats are not added multiple times and admin - the |
591 | 581 | owner - isn't added:: |
592 | | |
| 582 | |
593 | 583 | sage: W.collaborators() |
594 | 584 | ['hilbert', 'sage'] |
595 | 585 | """ |
… |
… |
class Worksheet(object): |
612 | 602 | def viewers(self): |
613 | 603 | """ |
614 | 604 | Return list of viewers of this worksheet. |
615 | | |
| 605 | |
616 | 606 | OUTPUT: |
617 | | |
618 | | |
| 607 | |
619 | 608 | - ``list`` - of string |
620 | | |
621 | | |
| 609 | |
622 | 610 | EXAMPLES:: |
623 | | |
624 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 611 | |
| 612 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
625 | 613 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
626 | 614 | sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True) |
627 | 615 | sage: W = nb.create_new_worksheet('test1', 'admin') |
… |
… |
class Worksheet(object): |
641 | 629 | def viewer_names(self, max=None): |
642 | 630 | """ |
643 | 631 | Returns a string of the non-owner viewers on this worksheet. |
644 | | |
| 632 | |
645 | 633 | INPUT: |
646 | | |
647 | | |
| 634 | |
648 | 635 | - ``max`` - an integer. If this is specified, then |
649 | 636 | only max number of viewers are shown. |
650 | | |
651 | | |
| 637 | |
652 | 638 | EXAMPLES:: |
653 | | |
654 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 639 | |
| 640 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
655 | 641 | sage: W = nb.create_new_worksheet('test1', 'admin') |
656 | 642 | sage: C = W.viewers(); C |
657 | 643 | [] |
… |
… |
class Worksheet(object): |
672 | 658 | Delete data from this worksheet this is specific to a certain |
673 | 659 | notebook. This means deleting the attached files, collaborators, |
674 | 660 | and viewers. |
675 | | |
| 661 | |
676 | 662 | EXAMPLES:: |
677 | | |
678 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 663 | |
| 664 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
679 | 665 | sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True) |
680 | 666 | sage: W = nb.create_new_worksheet('test1', 'admin') |
681 | 667 | sage: W.add_viewer('hilbert') |
… |
… |
class Worksheet(object): |
692 | 678 | self.__attached = {} |
693 | 679 | self.__collaborators = [self.owner()] |
694 | 680 | self.__viewers = [] |
695 | | |
| 681 | |
696 | 682 | def name(self): |
697 | 683 | """ |
698 | 684 | Return the name of this worksheet. |
699 | | |
| 685 | |
700 | 686 | OUTPUT: string |
701 | | |
| 687 | |
702 | 688 | EXAMPLES:: |
703 | | |
704 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 689 | |
| 690 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
705 | 691 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
706 | 692 | sage: W.name() |
707 | 693 | 'A Test Worksheet' |
… |
… |
class Worksheet(object): |
715 | 701 | def set_name(self, name): |
716 | 702 | """ |
717 | 703 | Set the name of this worksheet. |
718 | | |
| 704 | |
719 | 705 | INPUT: |
720 | | |
721 | | |
| 706 | |
722 | 707 | - ``name`` - string |
723 | | |
724 | | |
| 708 | |
725 | 709 | EXAMPLES: We create a worksheet and change the name:: |
726 | | |
727 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 710 | |
| 711 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
728 | 712 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
729 | 713 | sage: W.set_name('A renamed worksheet') |
730 | 714 | sage: W.name() |
… |
… |
class Worksheet(object): |
738 | 722 | r""" |
739 | 723 | Set this worksheet filename (actually directory) by getting the |
740 | 724 | owner from the pre-stored owner via ``self.owner()``. |
741 | | |
| 725 | |
742 | 726 | INPUT: |
743 | | |
744 | | |
| 727 | |
745 | 728 | - ``nm`` - string |
746 | | |
747 | | |
| 729 | |
748 | 730 | EXAMPLES:: |
749 | | |
750 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 731 | |
| 732 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
751 | 733 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
752 | 734 | sage: W.filename() |
753 | 735 | 'admin/0' |
… |
… |
class Worksheet(object): |
761 | 743 | def set_filename(self, filename): |
762 | 744 | """ |
763 | 745 | Set the worksheet filename (actually directory). |
764 | | |
| 746 | |
765 | 747 | INPUT: |
766 | | |
767 | | |
| 748 | |
768 | 749 | - ``filename`` - string |
769 | | |
770 | | |
| 750 | |
771 | 751 | EXAMPLES:: |
772 | | |
773 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 752 | |
| 753 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
774 | 754 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
775 | 755 | sage: W.filename() |
776 | 756 | 'admin/0' |
… |
… |
class Worksheet(object): |
780 | 760 | """ |
781 | 761 | old_filename = self.__filename |
782 | 762 | self.__filename = filename |
783 | | self.__dir = os.path.join(self.notebook().worksheet_directory(), filename) |
| 763 | self.__dir = os.path.join(self.notebook()._dir, filename) |
784 | 764 | self.notebook().change_worksheet_key(old_filename, filename) |
785 | 765 | |
786 | 766 | def filename(self): |
787 | 767 | """ |
788 | 768 | Return the filename (really directory) where the files associated |
789 | 769 | to this worksheet are stored. |
790 | | |
| 770 | |
791 | 771 | EXAMPLES:: |
792 | | |
793 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 772 | |
| 773 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
794 | 774 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
795 | 775 | sage: W.filename() |
796 | 776 | 'admin/0' |
797 | | sage: sorted(os.listdir(nb.directory() + '/worksheets/' + W.filename())) |
798 | | ['cells', 'snapshots'] |
| 777 | sage: os.path.isdir(os.path.join(nb._dir, 'home', W.filename())) |
| 778 | True |
799 | 779 | """ |
800 | 780 | return self.__filename |
801 | 781 | |
… |
… |
class Worksheet(object): |
803 | 783 | """ |
804 | 784 | Return the part of the worksheet filename after the last /, i.e., |
805 | 785 | without any information about the owner of this worksheet. |
806 | | |
| 786 | |
807 | 787 | EXAMPLES:: |
808 | | |
809 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 788 | |
| 789 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
810 | 790 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
811 | 791 | sage: W.filename_without_owner() |
812 | 792 | '0' |
… |
… |
class Worksheet(object): |
819 | 799 | """ |
820 | 800 | Return the full path to the directory where this worksheet is |
821 | 801 | stored. |
822 | | |
| 802 | |
823 | 803 | OUTPUT: string |
824 | | |
| 804 | |
825 | 805 | EXAMPLES:: |
826 | | |
827 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 806 | |
| 807 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
828 | 808 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
829 | 809 | sage: W.directory() |
830 | | '.../worksheets/admin/0' |
| 810 | '.../home/admin/0' |
831 | 811 | """ |
832 | 812 | return self.__dir |
833 | 813 | |
834 | 814 | def data_directory(self): |
835 | 815 | """ |
836 | 816 | Return path to directory where worksheet data is stored. |
837 | | |
| 817 | |
838 | 818 | OUTPUT: string |
839 | | |
| 819 | |
840 | 820 | EXAMPLES:: |
841 | | |
842 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 821 | |
| 822 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
843 | 823 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
844 | 824 | sage: W.data_directory() |
845 | | '.../worksheets/admin/0/data' |
| 825 | '.../home/admin/0/data' |
846 | 826 | """ |
847 | 827 | d = os.path.join(self.directory(), 'data') |
848 | 828 | if not os.path.exists(d): |
… |
… |
class Worksheet(object): |
853 | 833 | """ |
854 | 834 | Return a list of the file names of files in the worksheet data |
855 | 835 | directory. |
856 | | |
| 836 | |
857 | 837 | OUTPUT: list of strings |
858 | | |
| 838 | |
859 | 839 | EXAMPLES:: |
860 | | |
861 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 840 | |
| 841 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
862 | 842 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
863 | 843 | sage: W.attached_data_files() |
864 | 844 | [] |
… |
… |
class Worksheet(object): |
875 | 855 | """ |
876 | 856 | Return the directory in which the cells of this worksheet are |
877 | 857 | evaluated. |
878 | | |
| 858 | |
879 | 859 | OUTPUT: string |
880 | | |
| 860 | |
881 | 861 | EXAMPLES:: |
882 | | |
883 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 862 | |
| 863 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
884 | 864 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
885 | 865 | sage: W.cells_directory() |
886 | | '.../worksheets/admin/0/cells' |
| 866 | '.../home/admin/0/cells' |
887 | 867 | """ |
888 | 868 | path = os.path.join(self.directory(), 'cells') |
889 | 869 | if not os.path.exists(path): |
… |
… |
class Worksheet(object): |
893 | 873 | def notebook(self): |
894 | 874 | """ |
895 | 875 | Return the notebook that contains this worksheet. |
896 | | |
| 876 | |
897 | 877 | OUTPUT: a Notebook object. |
898 | | |
| 878 | |
899 | 879 | EXAMPLES: This really returns the Notebook object that is set as a |
900 | | global variable of the twist module. |
901 | | |
902 | | :: |
903 | | |
904 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 880 | global variable of the twist module.:: |
| 881 | |
| 882 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
905 | 883 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
906 | 884 | sage: W.notebook() |
907 | 885 | <...sagenb.notebook.notebook.Notebook...> |
… |
… |
class Worksheet(object): |
918 | 896 | """ |
919 | 897 | Return the math software system in which by default all input to |
920 | 898 | the notebook is evaluated. |
921 | | |
| 899 | |
922 | 900 | OUTPUT: string |
923 | | |
| 901 | |
924 | 902 | EXAMPLES:: |
925 | | |
926 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 903 | |
| 904 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
927 | 905 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
928 | 906 | sage: W.system() |
929 | 907 | 'sage' |
… |
… |
class Worksheet(object): |
946 | 924 | Sage installed to running a server from the same directory |
947 | 925 | without Sage installed. We might as well support this. |
948 | 926 | |
949 | | OUTPUT: |
950 | | |
951 | | - integer |
| 927 | OUTPUT: integer |
952 | 928 | """ |
953 | 929 | S = self.system() |
954 | 930 | names = self.notebook().system_names() |
… |
… |
class Worksheet(object): |
963 | 939 | """ |
964 | 940 | Set the math software system in which input is evaluated by |
965 | 941 | default. |
966 | | |
| 942 | |
967 | 943 | INPUT: |
968 | | |
969 | | |
| 944 | |
970 | 945 | - ``system`` - string (default: 'sage') |
971 | | |
972 | | |
| 946 | |
973 | 947 | EXAMPLES:: |
974 | | |
975 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 948 | |
| 949 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
976 | 950 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
977 | 951 | sage: W.set_system('magma') |
978 | 952 | sage: W.system() |
… |
… |
class Worksheet(object): |
983 | 957 | def pretty_print(self): |
984 | 958 | """ |
985 | 959 | Return True if output should be pretty printed by default. |
986 | | |
| 960 | |
987 | 961 | OUTPUT: |
988 | | |
989 | | |
| 962 | |
990 | 963 | - ``bool`` - True of False |
991 | | |
992 | | |
| 964 | |
993 | 965 | EXAMPLES:: |
994 | | |
995 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 966 | |
| 967 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
996 | 968 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
997 | 969 | sage: W.pretty_print() |
998 | 970 | False |
999 | 971 | sage: W.set_pretty_print('true') |
1000 | 972 | sage: W.pretty_print() |
1001 | 973 | True |
| 974 | sage: W.quit() |
| 975 | sage: nb.delete() |
1002 | 976 | """ |
1003 | 977 | try: |
1004 | 978 | return self.__pretty_print |
… |
… |
class Worksheet(object): |
1009 | 983 | def set_pretty_print(self, check='false'): |
1010 | 984 | """ |
1011 | 985 | Set whether or not output should be pretty printed by default. |
1012 | | |
| 986 | |
1013 | 987 | INPUT: |
1014 | | |
1015 | | |
| 988 | |
1016 | 989 | - ``check`` - string (default: 'false'); either 'true' |
1017 | 990 | or 'false'. |
1018 | | |
1019 | | |
| 991 | |
1020 | 992 | .. note:: |
1021 | 993 | |
1022 | 994 | The reason the input is a string and lower case instead of |
… |
… |
class Worksheet(object): |
1024 | 996 | JavaScript. (And, Jason Grout wrote this and didn't realize |
1025 | 997 | how unpythonic this design is - it should be redone to use |
1026 | 998 | True/False.) |
1027 | | |
| 999 | |
1028 | 1000 | EXAMPLES:: |
1029 | | |
1030 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1001 | |
| 1002 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1031 | 1003 | sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin') |
1032 | 1004 | sage: W.set_pretty_print('false') |
1033 | 1005 | sage: W.pretty_print() |
… |
… |
class Worksheet(object): |
1035 | 1007 | sage: W.set_pretty_print('true') |
1036 | 1008 | sage: W.pretty_print() |
1037 | 1009 | True |
| 1010 | sage: W.quit() |
| 1011 | sage: nb.delete() |
1038 | 1012 | """ |
1039 | 1013 | if check == 'false': |
1040 | 1014 | check=False |
… |
… |
class Worksheet(object): |
1042 | 1016 | check=True |
1043 | 1017 | self.__pretty_print = check |
1044 | 1018 | self.eval_asap_no_output("pretty_print_default(%r)"%(check)) |
1045 | | |
| 1019 | |
1046 | 1020 | ########################################################## |
1047 | 1021 | # Publication |
1048 | 1022 | ########################################################## |
1049 | 1023 | def is_auto_publish(self): |
1050 | 1024 | """ |
1051 | | Returns True if this worksheet should be automatically published. |
| 1025 | Returns True if this worksheet should be automatically published. |
1052 | 1026 | """ |
1053 | | try: |
| 1027 | try: |
1054 | 1028 | return self.__autopublish |
1055 | 1029 | except AttributeError: |
1056 | 1030 | self.__autopublish = False |
1057 | 1031 | return False |
1058 | | |
| 1032 | |
1059 | 1033 | def set_auto_publish(self, x): |
1060 | 1034 | self.__autopublish = x |
1061 | | |
| 1035 | |
1062 | 1036 | def is_published(self): |
1063 | 1037 | """ |
1064 | 1038 | Return True if this worksheet is a published worksheet. |
1065 | | |
| 1039 | |
1066 | 1040 | OUTPUT: |
1067 | | |
1068 | | |
| 1041 | |
1069 | 1042 | - ``bool`` - whether or not owner is 'pub' |
1070 | | |
1071 | | |
| 1043 | |
1072 | 1044 | EXAMPLES:: |
1073 | | |
1074 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1045 | |
| 1046 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1075 | 1047 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1076 | 1048 | sage: W.is_published() |
1077 | 1049 | False |
… |
… |
class Worksheet(object): |
1086 | 1058 | Return owner and id_number of the worksheet that was published |
1087 | 1059 | to get this worksheet, if this worksheet was |
1088 | 1060 | published. Otherwise just return this worksheet. |
1089 | | |
| 1061 | |
1090 | 1062 | OUTPUT: Worksheet |
1091 | | |
| 1063 | |
1092 | 1064 | EXAMPLES:: |
1093 | | |
1094 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1065 | |
| 1066 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
1095 | 1067 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1096 | 1068 | sage: W.worksheet_that_was_published() is W |
1097 | 1069 | True |
1098 | | |
1099 | | :: |
1100 | | |
1101 | 1070 | sage: S = nb.publish_worksheet(W, 'admin') |
1102 | 1071 | sage: S.worksheet_that_was_published() is S |
1103 | 1072 | False |
… |
… |
class Worksheet(object): |
1108 | 1077 | return self.notebook().get_worksheet_with_filename('%s/%s'%self.__worksheet_came_from) |
1109 | 1078 | except Exception: # things can go wrong (especially with old migrated |
1110 | 1079 | # Sage notebook servers!), but we don't want such |
1111 | | # problems to crash the notebook server. |
| 1080 | # problems to crash the notebook server. |
1112 | 1081 | return self |
1113 | 1082 | |
1114 | 1083 | def publisher(self): |
1115 | 1084 | """ |
1116 | 1085 | Return username of user that published this worksheet. |
1117 | | |
| 1086 | |
1118 | 1087 | OUTPUT: string |
1119 | | |
| 1088 | |
1120 | 1089 | EXAMPLES:: |
1121 | | |
1122 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1090 | |
| 1091 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1123 | 1092 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1124 | 1093 | sage: S = nb.publish_worksheet(W, 'admin') |
1125 | 1094 | sage: S.publisher() |
… |
… |
class Worksheet(object): |
1131 | 1100 | """ |
1132 | 1101 | Return True if username is the username of the publisher of this |
1133 | 1102 | worksheet, assuming this worksheet was published. |
1134 | | |
| 1103 | |
1135 | 1104 | INPUT: |
1136 | | |
1137 | | |
| 1105 | |
1138 | 1106 | - ``username`` - string |
1139 | | |
1140 | | |
| 1107 | |
1141 | 1108 | EXAMPLES:: |
1142 | | |
1143 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1109 | |
| 1110 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
1144 | 1111 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1145 | 1112 | sage: P = nb.publish_worksheet(W, 'admin') |
1146 | 1113 | sage: P.is_publisher('hearst') |
… |
… |
class Worksheet(object): |
1153 | 1120 | def has_published_version(self): |
1154 | 1121 | """ |
1155 | 1122 | Return True if there is a published version of this worksheet. |
1156 | | |
| 1123 | |
1157 | 1124 | OUTPUT: bool |
1158 | | |
| 1125 | |
1159 | 1126 | EXAMPLES:: |
1160 | | |
1161 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1127 | |
| 1128 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1162 | 1129 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1163 | 1130 | sage: P = nb.publish_worksheet(W, 'admin') |
1164 | 1131 | sage: P.has_published_version() |
… |
… |
class Worksheet(object): |
1176 | 1143 | """ |
1177 | 1144 | Set the published version of this worksheet to be the worksheet |
1178 | 1145 | with given filename. |
1179 | | |
| 1146 | |
1180 | 1147 | INPUT: |
1181 | | |
1182 | | |
| 1148 | |
1183 | 1149 | - ``filename`` - string |
1184 | | |
1185 | | |
| 1150 | |
1186 | 1151 | EXAMPLES:: |
1187 | | |
1188 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1152 | |
| 1153 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1189 | 1154 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1190 | 1155 | sage: P = nb.publish_worksheet(W, 'admin') # indirect test |
1191 | 1156 | sage: W._Worksheet__published_version |
| 1157 | 'pub/1' |
| 1158 | sage: W.set_published_version('pub/0') |
| 1159 | sage: W._Worksheet__published_version |
1192 | 1160 | 'pub/0' |
1193 | | sage: W.set_published_version('pub/0') |
1194 | 1161 | """ |
1195 | 1162 | self.__published_version = filename |
1196 | 1163 | |
… |
… |
class Worksheet(object): |
1198 | 1165 | """ |
1199 | 1166 | If this worksheet was published, return the published version of |
1200 | 1167 | this worksheet. Otherwise, raise a ValueError. |
1201 | | |
| 1168 | |
1202 | 1169 | OUTPUT: a worksheet (or raise a ValueError) |
1203 | | |
| 1170 | |
1204 | 1171 | EXAMPLES:: |
1205 | | |
1206 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1172 | |
| 1173 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
1207 | 1174 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1208 | 1175 | sage: P = nb.publish_worksheet(W, 'admin') |
1209 | 1176 | sage: W.published_version() is P |
1210 | 1177 | True |
1211 | | """ |
| 1178 | """ |
1212 | 1179 | try: |
1213 | 1180 | filename =self.__published_version |
1214 | 1181 | try: |
… |
… |
class Worksheet(object): |
1219 | 1186 | raise ValueError |
1220 | 1187 | except AttributeError: |
1221 | 1188 | raise ValueError, "no published version" |
1222 | | |
| 1189 | |
1223 | 1190 | def set_worksheet_that_was_published(self, W): |
1224 | 1191 | """ |
1225 | 1192 | Set the owner and id_number of the worksheet that was |
1226 | 1193 | published to get self. |
1227 | | |
| 1194 | |
1228 | 1195 | INPUT: |
1229 | 1196 | |
1230 | 1197 | - ``W`` -- worksheet or 2-tuple ('owner', id_number) |
1231 | 1198 | |
1232 | | |
1233 | 1199 | EXAMPLES:: |
1234 | | |
1235 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1200 | |
| 1201 | sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb') |
1236 | 1202 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1237 | 1203 | sage: P = nb.publish_worksheet(W, 'admin') |
1238 | 1204 | sage: P.worksheet_that_was_published() is W |
1239 | 1205 | True |
1240 | | |
| 1206 | |
1241 | 1207 | We fake things and make it look like P published itself:: |
1242 | | |
| 1208 | |
1243 | 1209 | sage: P.set_worksheet_that_was_published(P) |
1244 | 1210 | sage: P.worksheet_that_was_published() is P |
1245 | 1211 | True |
… |
… |
class Worksheet(object): |
1253 | 1219 | """ |
1254 | 1220 | Set the rating on this worksheet by the given user to x and also |
1255 | 1221 | set the given comment. |
1256 | | |
| 1222 | |
1257 | 1223 | INPUT: |
1258 | | |
1259 | | |
| 1224 | |
1260 | 1225 | - ``x`` - integer |
1261 | | |
| 1226 | |
1262 | 1227 | - ``comment`` - string |
1263 | | |
| 1228 | |
1264 | 1229 | - ``username`` - string |
1265 | | |
1266 | | |
| 1230 | |
1267 | 1231 | EXAMPLES: We create a worksheet and rate it, then look at the |
1268 | | ratings. |
1269 | | |
1270 | | :: |
1271 | | |
1272 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1232 | ratings.:: |
| 1233 | |
| 1234 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1273 | 1235 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1274 | 1236 | sage: W.rate(3, 'this is great', 'hilbert') |
1275 | 1237 | sage: W.ratings() |
1276 | 1238 | [('hilbert', 3, 'this is great')] |
1277 | | |
| 1239 | |
1278 | 1240 | Note that only the last rating by a user counts:: |
1279 | | |
| 1241 | |
1280 | 1242 | sage: W.rate(1, 'this lacks content', 'riemann') |
1281 | 1243 | sage: W.rate(0, 'this lacks content', 'riemann') |
1282 | 1244 | sage: W.ratings() |
… |
… |
class Worksheet(object): |
1294 | 1256 | """ |
1295 | 1257 | Return True is the user with given username has rated this |
1296 | 1258 | worksheet. |
1297 | | |
| 1259 | |
1298 | 1260 | INPUT: |
1299 | | |
1300 | | |
| 1261 | |
1301 | 1262 | - ``username`` - string |
1302 | | |
1303 | | |
| 1263 | |
1304 | 1264 | OUTPUT: bool |
1305 | | |
| 1265 | |
1306 | 1266 | EXAMPLES:: |
1307 | | |
1308 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1267 | |
| 1268 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1309 | 1269 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1310 | 1270 | sage: W.rate(0, 'this lacks content', 'riemann') |
1311 | 1271 | sage: W.is_rater('admin') |
… |
… |
class Worksheet(object): |
1321 | 1281 | def ratings(self): |
1322 | 1282 | """ |
1323 | 1283 | Return all the ratings of this worksheet. |
1324 | | |
| 1284 | |
1325 | 1285 | OUTPUT: |
1326 | | |
1327 | | |
| 1286 | |
1328 | 1287 | - ``list`` - a reference to the list of ratings. |
1329 | | |
1330 | | |
| 1288 | |
1331 | 1289 | EXAMPLES:: |
1332 | | |
1333 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1290 | |
| 1291 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1334 | 1292 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1335 | 1293 | sage: W.ratings() |
1336 | 1294 | [] |
… |
… |
class Worksheet(object): |
1350 | 1308 | r""" |
1351 | 1309 | Return html that renders to give a summary of how this worksheet |
1352 | 1310 | has been rated. |
1353 | | |
| 1311 | |
1354 | 1312 | OUTPUT: |
1355 | | |
1356 | | |
1357 | | - ``string`` -- a string of HTML as a bunch of table |
1358 | | rows. |
1359 | | |
1360 | | |
| 1313 | |
| 1314 | - ``string`` -- a string of HTML as a bunch of table rows. |
| 1315 | |
1361 | 1316 | EXAMPLES:: |
1362 | | |
1363 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1317 | |
| 1318 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1364 | 1319 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1365 | 1320 | sage: W.rate(0, 'this lacks content', 'riemann') |
1366 | 1321 | sage: W.rate(3, 'this is great', 'hilbert') |
… |
… |
class Worksheet(object): |
1382 | 1337 | def rating(self): |
1383 | 1338 | """ |
1384 | 1339 | Return overall average rating of self. |
1385 | | |
| 1340 | |
1386 | 1341 | OUTPUT: float or the int -1 to mean "not rated" |
1387 | | |
| 1342 | |
1388 | 1343 | EXAMPLES:: |
1389 | | |
1390 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1344 | |
| 1345 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1391 | 1346 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1392 | 1347 | sage: W.rating() |
1393 | 1348 | -1 |
… |
… |
class Worksheet(object): |
1402 | 1357 | else: |
1403 | 1358 | rating = float(sum(r))/float(len(r)) |
1404 | 1359 | return rating |
1405 | | |
| 1360 | |
1406 | 1361 | ########################################################## |
1407 | 1362 | # Active, trash can and archive |
1408 | 1363 | ########################################################## |
… |
… |
class Worksheet(object): |
1410 | 1365 | """ |
1411 | 1366 | Return True if all users have deleted this worksheet, so we know we |
1412 | 1367 | can safely purge it from disk. |
1413 | | |
| 1368 | |
1414 | 1369 | OUTPUT: bool |
1415 | | |
| 1370 | |
1416 | 1371 | EXAMPLES:: |
1417 | | |
1418 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1372 | |
| 1373 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1419 | 1374 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1420 | 1375 | sage: W.everyone_has_deleted_this_worksheet() |
1421 | 1376 | False |
… |
… |
class Worksheet(object): |
1423 | 1378 | sage: W.everyone_has_deleted_this_worksheet() |
1424 | 1379 | True |
1425 | 1380 | """ |
1426 | | for user in self.__collaborators + [self.owner()]: |
| 1381 | for user in self.collaborators() + [self.owner()]: |
1427 | 1382 | # When the worksheet has been deleted by the owner, |
1428 | 1383 | # self.owner() returns None, so we have to be careful |
1429 | 1384 | # about that case. |
1430 | 1385 | if user is not None and not self.is_trashed(user): |
1431 | 1386 | return False |
1432 | 1387 | return True |
1433 | | |
| 1388 | |
1434 | 1389 | def user_view(self, user): |
1435 | 1390 | """ |
1436 | 1391 | Return the view that the given user has of this worksheet. If the |
1437 | 1392 | user currently doesn't have a view set it to ACTIVE and return |
1438 | 1393 | ACTIVE. |
1439 | | |
| 1394 | |
1440 | 1395 | INPUT: |
1441 | | |
1442 | | |
| 1396 | |
1443 | 1397 | - ``user`` - a string |
1444 | | |
1445 | | |
| 1398 | |
1446 | 1399 | OUTPUT: |
1447 | | |
1448 | | |
| 1400 | |
1449 | 1401 | - ``Python int`` - one of ACTIVE, ARCHIVED, TRASH, |
1450 | 1402 | which are defined in worksheet.py |
1451 | | |
1452 | | |
| 1403 | |
1453 | 1404 | EXAMPLES: We create a new worksheet and get the view, which is |
1454 | 1405 | ACTIVE:: |
1455 | | |
1456 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1406 | |
| 1407 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1457 | 1408 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1458 | 1409 | sage: W.user_view('admin') |
1459 | 1410 | 1 |
1460 | 1411 | sage: sagenb.notebook.worksheet.ACTIVE |
1461 | 1412 | 1 |
1462 | | |
| 1413 | |
1463 | 1414 | Now for the admin user we move W to the archive:: |
1464 | | |
| 1415 | |
1465 | 1416 | sage: W.move_to_archive('admin') |
1466 | | |
1467 | | The view is now archive. |
1468 | | |
1469 | | :: |
1470 | | |
| 1417 | |
| 1418 | The view is now archive.:: |
| 1419 | |
1471 | 1420 | sage: W.user_view('admin') |
1472 | 1421 | 0 |
1473 | 1422 | sage: sagenb.notebook.worksheet.ARCHIVED |
1474 | 1423 | 0 |
1475 | | |
1476 | | For any other random viewer the view is set by default to ACTIVE. |
1477 | | |
1478 | | :: |
1479 | | |
| 1424 | |
| 1425 | For any other random viewer the view is set by default to ACTIVE.:: |
| 1426 | |
1480 | 1427 | sage: W.user_view('foo') |
1481 | 1428 | 1 |
1482 | 1429 | """ |
… |
… |
class Worksheet(object): |
1497 | 1444 | d = dict(self.__user_view) |
1498 | 1445 | except AttributeError: |
1499 | 1446 | self.user_view(self.owner()) |
1500 | | d = self.__user_view |
| 1447 | d = copy.copy(self.__user_view) |
1501 | 1448 | for user, val in d.iteritems(): |
1502 | | d[user] = [val] |
| 1449 | if not isinstance(val, list): |
| 1450 | d[user] = [val] |
1503 | 1451 | return d |
1504 | 1452 | |
1505 | 1453 | def set_tags(self, tags): |
1506 | 1454 | """ |
1507 | | Set the tags -- for now we ignore everything except ACTIVE, ARCHIVED, TRASH. |
| 1455 | Set the tags -- for now we ignore everything except ACTIVE, |
| 1456 | ARCHIVED, TRASH. |
1508 | 1457 | |
1509 | 1458 | INPUT: |
1510 | 1459 | |
… |
… |
class Worksheet(object): |
1521 | 1470 | def set_user_view(self, user, x): |
1522 | 1471 | """ |
1523 | 1472 | Set the view on this worksheet for the given user. |
1524 | | |
| 1473 | |
1525 | 1474 | INPUT: |
1526 | | |
1527 | | |
| 1475 | |
1528 | 1476 | - ``user`` - a string |
1529 | | |
| 1477 | |
1530 | 1478 | - ``x`` - int, one of the variables ACTIVE, ARCHIVED, |
1531 | 1479 | TRASH in worksheet.py |
1532 | | |
1533 | | |
| 1480 | |
1534 | 1481 | EXAMPLES:: |
1535 | | |
1536 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1482 | |
| 1483 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1537 | 1484 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1538 | 1485 | sage: W.set_user_view('admin', sagenb.notebook.worksheet.ARCHIVED) |
1539 | 1486 | sage: W.user_view('admin') == sagenb.notebook.worksheet.ARCHIVED |
… |
… |
class Worksheet(object): |
1555 | 1502 | def user_view_is(self, user, x): |
1556 | 1503 | """ |
1557 | 1504 | Return True if the user view of user is x. |
1558 | | |
| 1505 | |
1559 | 1506 | INPUT: |
1560 | | |
1561 | | |
| 1507 | |
1562 | 1508 | - ``user`` - a string |
1563 | | |
| 1509 | |
1564 | 1510 | - ``x`` - int, one of the variables ACTIVE, ARCHIVED, |
1565 | 1511 | TRASH in worksheet.py |
1566 | | |
1567 | | |
| 1512 | |
1568 | 1513 | EXAMPLES:: |
1569 | | |
1570 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1514 | |
| 1515 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1571 | 1516 | sage: W = nb.create_new_worksheet('Publish Test', 'admin') |
1572 | 1517 | sage: W.user_view_is('admin', sagenb.notebook.worksheet.ARCHIVED) |
1573 | 1518 | False |
… |
… |
class Worksheet(object): |
1575 | 1520 | True |
1576 | 1521 | """ |
1577 | 1522 | return self.user_view(user) == x |
1578 | | |
| 1523 | |
1579 | 1524 | def is_archived(self, user): |
1580 | 1525 | """ |
1581 | 1526 | Return True if this worksheet is archived for the given user. |
1582 | | |
| 1527 | |
1583 | 1528 | INPUT: |
1584 | | |
1585 | | |
| 1529 | |
1586 | 1530 | - ``user`` - string |
1587 | | |
1588 | | |
| 1531 | |
1589 | 1532 | OUTPUT: bool |
1590 | | |
| 1533 | |
1591 | 1534 | EXAMPLES:: |
1592 | | |
1593 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1535 | |
| 1536 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1594 | 1537 | sage: W = nb.create_new_worksheet('Archived Test', 'admin') |
1595 | 1538 | sage: W.is_archived('admin') |
1596 | 1539 | False |
… |
… |
class Worksheet(object): |
1603 | 1546 | def is_active(self, user): |
1604 | 1547 | """ |
1605 | 1548 | Return True if this worksheet is active for the given user. |
1606 | | |
| 1549 | |
1607 | 1550 | INPUT: |
1608 | | |
1609 | | |
| 1551 | |
1610 | 1552 | - ``user`` - string |
1611 | | |
1612 | | |
| 1553 | |
1613 | 1554 | OUTPUT: bool |
1614 | | |
| 1555 | |
1615 | 1556 | EXAMPLES:: |
1616 | | |
1617 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1557 | |
| 1558 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1618 | 1559 | sage: W = nb.create_new_worksheet('Active Test', 'admin') |
1619 | 1560 | sage: W.is_active('admin') |
1620 | 1561 | True |
… |
… |
class Worksheet(object): |
1627 | 1568 | def is_trashed(self, user): |
1628 | 1569 | """ |
1629 | 1570 | Return True if this worksheet is in the trash for the given user. |
1630 | | |
| 1571 | |
1631 | 1572 | INPUT: |
1632 | | |
1633 | | |
| 1573 | |
1634 | 1574 | - ``user`` - string |
1635 | | |
1636 | | |
| 1575 | |
1637 | 1576 | OUTPUT: bool |
1638 | | |
| 1577 | |
1639 | 1578 | EXAMPLES:: |
1640 | | |
1641 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1579 | |
| 1580 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1642 | 1581 | sage: W = nb.create_new_worksheet('Trash Test', 'admin') |
1643 | 1582 | sage: W.is_trashed('admin') |
1644 | 1583 | False |
… |
… |
class Worksheet(object): |
1651 | 1590 | def move_to_archive(self, user): |
1652 | 1591 | """ |
1653 | 1592 | Move this worksheet to be archived for the given user. |
1654 | | |
| 1593 | |
1655 | 1594 | INPUT: |
1656 | | |
1657 | | |
| 1595 | |
1658 | 1596 | - ``user`` - string |
1659 | | |
1660 | | |
| 1597 | |
1661 | 1598 | EXAMPLES:: |
1662 | | |
1663 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1599 | |
| 1600 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1664 | 1601 | sage: W = nb.create_new_worksheet('Archive Test', 'admin') |
1665 | 1602 | sage: W.move_to_archive('admin') |
1666 | 1603 | sage: W.is_archived('admin') |
… |
… |
class Worksheet(object): |
1671 | 1608 | def set_active(self, user): |
1672 | 1609 | """ |
1673 | 1610 | Set his worksheet to be active for the given user. |
1674 | | |
| 1611 | |
1675 | 1612 | INPUT: |
1676 | | |
1677 | | |
| 1613 | |
1678 | 1614 | - ``user`` - string |
1679 | | |
1680 | | |
| 1615 | |
1681 | 1616 | EXAMPLES:: |
1682 | | |
1683 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1617 | |
| 1618 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1684 | 1619 | sage: W = nb.create_new_worksheet('Active Test', 'admin') |
1685 | 1620 | sage: W.move_to_archive('admin') |
1686 | 1621 | sage: W.is_active('admin') |
… |
… |
class Worksheet(object): |
1694 | 1629 | def move_to_trash(self, user): |
1695 | 1630 | """ |
1696 | 1631 | Move this worksheet to the trash for the given user. |
1697 | | |
| 1632 | |
1698 | 1633 | INPUT: |
1699 | | |
1700 | | |
| 1634 | |
1701 | 1635 | - ``user`` - string |
1702 | | |
1703 | | |
| 1636 | |
1704 | 1637 | EXAMPLES:: |
1705 | | |
1706 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1638 | |
| 1639 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1707 | 1640 | sage: W = nb.create_new_worksheet('Trash Test', 'admin') |
1708 | 1641 | sage: W.move_to_trash('admin') |
1709 | 1642 | sage: W.is_trashed('admin') |
… |
… |
class Worksheet(object): |
1714 | 1647 | def move_out_of_trash(self, user): |
1715 | 1648 | """ |
1716 | 1649 | Exactly the same as set_active(user). |
1717 | | |
| 1650 | |
1718 | 1651 | INPUT: |
1719 | | |
1720 | | |
| 1652 | |
1721 | 1653 | - ``user`` - string |
1722 | | |
1723 | | |
| 1654 | |
1724 | 1655 | EXAMPLES:: |
1725 | | |
1726 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1656 | |
| 1657 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1727 | 1658 | sage: W = nb.create_new_worksheet('Active Test', 'admin') |
1728 | 1659 | sage: W.move_to_trash('admin') |
1729 | 1660 | sage: W.is_active('admin') |
… |
… |
class Worksheet(object): |
1733 | 1664 | True |
1734 | 1665 | """ |
1735 | 1666 | self.set_active(user) |
1736 | | |
| 1667 | |
1737 | 1668 | ############# |
1738 | 1669 | |
1739 | 1670 | def delete_cells_directory(self): |
1740 | 1671 | r""" |
1741 | 1672 | Delete the directory in which all the cell computations occur. |
1742 | | |
| 1673 | |
1743 | 1674 | EXAMPLES:: |
1744 | | |
1745 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1675 | |
| 1676 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1746 | 1677 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1747 | 1678 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1748 | | sage: W.edit_save('Sage\n{{{\n3^20\n}}}') |
| 1679 | sage: W.edit_save('{{{\n3^20\n}}}') |
| 1680 | sage: W.cell_list()[0].evaluate() |
| 1681 | sage: W.check_comp() # random output -- depends on computer speed |
1749 | 1682 | sage: sorted(os.listdir(W.directory())) |
1750 | | ['cells', 'snapshots'] |
1751 | | sage: W.cell_list()[0].evaluate() |
| 1683 | ['cells', 'data'] |
| 1684 | sage: W.save_snapshot('admin') |
1752 | 1685 | sage: sorted(os.listdir(W.directory())) |
1753 | | ['cells', 'data', 'snapshots'] |
| 1686 | ['cells', 'data', 'snapshots', 'worksheet.html'] |
1754 | 1687 | sage: W.delete_cells_directory() |
1755 | 1688 | sage: sorted(os.listdir(W.directory())) |
1756 | | ['data', 'snapshots'] |
| 1689 | ['data', 'snapshots', 'worksheet.html'] |
| 1690 | sage: W.quit() |
| 1691 | sage: nb.delete() |
1757 | 1692 | """ |
1758 | 1693 | dir = self.cells_directory() |
1759 | 1694 | if os.path.exists(dir): |
… |
… |
class Worksheet(object): |
1776 | 1711 | |
1777 | 1712 | def set_owner(self, owner): |
1778 | 1713 | self.__owner = owner |
1779 | | if not owner in self.__collaborators: |
| 1714 | if not owner in self.collaborators(): |
1780 | 1715 | self.__collaborators.append(owner) |
1781 | 1716 | |
1782 | 1717 | def is_only_viewer(self, user): |
… |
… |
class Worksheet(object): |
1792 | 1727 | return True |
1793 | 1728 | |
1794 | 1729 | def is_collaborator(self, user): |
1795 | | try: |
1796 | | return user in self.__collaborators |
1797 | | except AttributeError: |
1798 | | return True |
| 1730 | return user in self.collaborators() |
1799 | 1731 | |
1800 | 1732 | def user_can_edit(self, user): |
1801 | 1733 | """ |
1802 | 1734 | Return True if the user with given name is allowed to edit this |
1803 | 1735 | worksheet. |
1804 | | |
| 1736 | |
1805 | 1737 | INPUT: |
1806 | | |
1807 | | |
| 1738 | |
1808 | 1739 | - ``user`` - string |
1809 | | |
1810 | | |
| 1740 | |
1811 | 1741 | OUTPUT: bool |
1812 | | |
1813 | | EXAMPLES: We create a notebook with one worksheet and two users. |
1814 | | |
1815 | | :: |
1816 | | |
1817 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1742 | |
| 1743 | EXAMPLES: We create a notebook with one worksheet and two users.:: |
| 1744 | |
| 1745 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1818 | 1746 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1819 | 1747 | sage: nb.add_user('william', 'william', 'wstein@sagemath.org', force=True) |
1820 | 1748 | sage: W = nb.create_new_worksheet('Test', 'sage') |
1821 | 1749 | sage: W.user_can_edit('sage') |
1822 | 1750 | True |
1823 | | |
| 1751 | |
1824 | 1752 | At first the user 'william' can't edit this worksheet:: |
1825 | | |
| 1753 | |
1826 | 1754 | sage: W.user_can_edit('william') |
1827 | 1755 | False |
1828 | | |
| 1756 | |
1829 | 1757 | After adding 'william' as a collaborator he can edit the |
1830 | | worksheet. |
1831 | | |
1832 | | :: |
1833 | | |
| 1758 | worksheet.:: |
| 1759 | |
1834 | 1760 | sage: W.add_collaborator('william') |
1835 | 1761 | sage: W.user_can_edit('william') |
1836 | 1762 | True |
1837 | | |
| 1763 | |
1838 | 1764 | Clean up:: |
1839 | | |
| 1765 | |
1840 | 1766 | sage: nb.delete() |
1841 | 1767 | """ |
1842 | 1768 | return self.is_collaborator(user) or self.is_owner(user) |
1843 | | |
| 1769 | |
1844 | 1770 | def delete_user(self, user): |
1845 | 1771 | """ |
1846 | 1772 | Delete a user from having any view or ownership of this worksheet. |
1847 | | |
| 1773 | |
1848 | 1774 | INPUT: |
1849 | | |
1850 | | |
| 1775 | |
1851 | 1776 | - ``user`` - string; the name of a user |
1852 | | |
1853 | | |
| 1777 | |
1854 | 1778 | EXAMPLES: We create a notebook with 2 users and 1 worksheet that |
1855 | | both view. |
1856 | | |
1857 | | :: |
1858 | | |
1859 | | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()) |
| 1779 | both view.:: |
| 1780 | |
| 1781 | sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb') |
1860 | 1782 | sage: nb.add_user('wstein','sage','wstein@sagemath.org',force=True) |
1861 | 1783 | sage: nb.add_user('sage','sage','sage@sagemath.org',force=True) |
1862 | 1784 | sage: W = nb.new_worksheet_with_title_from_text('Sage', owner='sage') |
… |
… |
class Worksheet(object): |
1865 | 1787 | 'sage' |
1866 | 1788 | sage: W.viewers() |
1867 | 1789 | ['wstein'] |
1868 | | |
| 1790 | |
1869 | 1791 | We delete the sage user from the worksheet W. This makes wstein the |
1870 | | new owner. |
1871 | | |
1872 | | :: |
1873 | | |
| 1792 | new owner.:: |
| 1793 | |
1874 | 1794 | sage: W.delete_user('sage') |
1875 | 1795 | sage: W.viewers() |
1876 | 1796 | ['wstein'] |
1877 | 1797 | sage: W.owner() |
1878 | 1798 | 'wstein' |
1879 | | |
| 1799 | |