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