annotate scripts/miscellaneous/jupyter_notebook.m @ 30294:488548c762de

jupyter_notebook: don't hardwire tmp directory name (bug #61470) * scripts/miscellaneous/jupyter_notebook.m (jupyter_notebook.tmpdir): New property. Use it instead of "__octave_jupyter_temp__".
author John W. Eaton <jwe@octave.org>
date Mon, 15 Nov 2021 17:41:07 +0900
parents 1a1f3ae76e74
children 212461a26e9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
1 ## Copyright (C) 2021 The Octave Project Developers
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
2 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
3 ## This program is free software: you can redistribute it and/or modify it
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
4 ## under the terms of the GNU General Public License as published by
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
5 ## the Free Software Foundation, either version 3 of the License, or
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
6 ## (at your option) any later version.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
7 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful, but
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
11 ## GNU General Public License for more details.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
12 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
14 ## along with this program. If not, see
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
15 ## <https://www.gnu.org/licenses/>.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
16
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
17
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
18 classdef jupyter_notebook < handle
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
19
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
20 ## -*- texinfo -*-
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
21 ## @deftypefn {} {@var{notebook} =} jupyter_notebook (@var{notebook_file_name})
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
22 ## @deftypefnx {} {@var{notebook} =} jupyter_notebook (@var{notebook_file_name}, @var{options})
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
23 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
24 ## Run and fill the Jupyter Notebook in @var{notebook_file_name} within
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
25 ## GNU Octave.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
26 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
27 ## Supported are textual and graphical Octave outputs.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
28 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
29 ## This class has a public attribute @qcode{notebook} which is a struct
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
30 ## representing the JSON-decoded Jupyter Notebook. This attribute is
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
31 ## intentionally public to enable advanced notebook manipulations.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
32 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
33 ## Note: Jupyter Notebook versions (@qcode{nbformat}) lower than 4.0 are
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
34 ## not supported.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
35 ##
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
36 ## The second argument @var{options} is a struct with fields:
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
37 ## @itemize @bullet
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
38 ## @item
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
39 ## @qcode{tmpdir} to set the temporary working directory.
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
40 ## @end itemize
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
41 ##
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
42 ## @qcode{%plot} magic is supported with the following settings:
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
43 ## @itemize @bullet
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
44 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
45 ## "%plot -f <format>" or "%plot --format <format>": specifies the
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
46 ## image storage format. Supported formats are:
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
47 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
48 ## @itemize @minus
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
49 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
50 ## PNG (default)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
51 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
52 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
53 ## SVG (Note: If SVG images do not appear in the notebook, it is most
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
54 ## related to the Jupyter Notebook security mechanism and explicitly
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
55 ## "trusting" them is necessary).
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
56 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
57 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
58 ## JPG
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
59 ## @end itemize
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
60 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
61 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
62 ## "%plot -r <number>" or "%plot --resolution <number>": specifies the
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
63 ## image resolution.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
64 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
65 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
66 ## "%plot -w <number>" or "%plot --width <number>": specifies the
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
67 ## image width.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
68 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
69 ## @item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
70 ## "%plot -h <number>" or "%plot --height <number>": specifies the
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
71 ## image height.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
72 ## @end itemize
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
73 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
74 ## Examples:
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
75 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
76 ## @example
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
77 ## @group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
78 ## ## Run all cells and generate the filled notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
79 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
80 ## ## Instantiate an object from the notebook file
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
81 ## notebook = jupyter_notebook("myNotebook.ipynb")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
82 ## @result{} notebook =
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
83 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
84 ## <object jupyter_notebook>
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
85 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
86 ## ## Run the code and embed the results in the @qcode{notebook} attribute
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
87 ## notebook.run_all()
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
88 ## ## Generate the new notebook by overwriting the original notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
89 ## notebook.generate_notebook("myNotebook.ipynb")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
90 ## @end group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
91 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
92 ## @group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
93 ## ## Run the second cell and generate the filled notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
94 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
95 ## ## Instantiate an object from the notebook file
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
96 ## notebook = jupyter_notebook("myNotebook.ipynb")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
97 ## @result{} notebook =
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
98 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
99 ## <object jupyter_notebook>
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
100 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
101 ## ## Run the code and embed the results in the @qcode{notebook} attribute
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
102 ## notebook.run(2)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
103 ## ## Generate the new notebook in a new file
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
104 ## notebook.generate_notebook("myNewNotebook.ipynb")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
105 ## @end group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
106 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
107 ## @group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
108 ## ## Generate an Octave script from a notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
109 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
110 ## ## Instantiate an object from the notebook file
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
111 ## notebook = jupyter_notebook("myNotebook.ipynb")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
112 ## @result{} notebook =
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
113 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
114 ## <object jupyter_notebook>
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
115 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
116 ## ## Generate the octave script
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
117 ## notebook.generate_octave_script("myScript.m")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
118 ## @end group
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
119 ## @end example
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
120 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
121 ## @seealso{jsondecode, jsonencode}
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
122 ## @end deftypefn
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
123
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
124 properties
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
125
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
126 notebook = struct()
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
127
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
128 endproperties
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
129
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
130 properties (Access = "private")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
131
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
132 context = struct("ans", "")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
133
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
134 ## Note: This name needs to be stored in a property because it is
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
135 ## set in the constructor but used in some other methods. However,
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
136 ## we want to defer calling tempname() until immediately before
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
137 ## calling mkdir. The temporary directory currently created and
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
138 ## deleted in the constructor and the name is reset to the empty
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
139 ## string when the directory is deleted. Another possible
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
140 ## implementation might be to generate the name and create the
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
141 ## temporary directory here, then delete it in the class destructor.
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
142
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
143 tmpdir = "";
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
144
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
145 endproperties
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
146
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
147 methods
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
148
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
149 function obj = jupyter_notebook (notebook_file_name, options)
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
150
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
151 if ((nargin < 1) || (nargin > 2))
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
152 print_usage ();
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
153 endif
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
154
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
155 ## Validate options if present.
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
156 if ((nargin == 2) && ! isstruct (options))
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
157 error ("jupyter_notebook: options must be a struct");
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
158 endif
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
159 if ((nargin == 2) && (isfield (options, "tmpdir")))
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
160 obj.tmpdir = options.tmpdir;
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
161 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
162
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
163 if (! (ischar (notebook_file_name) && isrow (notebook_file_name)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
164 error ("jupyter_notebook: notebook_file_name must be a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
165 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
166
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
167 obj.notebook = jsondecode (fileread (notebook_file_name),
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
168 "makeValidName", false);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
169
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
170 ## Validate the notebook's format according to nbformat: 4.0
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
171 if (! (isfield (obj.notebook, "metadata")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
172 && isfield (obj.notebook, "nbformat")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
173 && isfield (obj.notebook, "nbformat_minor")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
174 && isfield (obj.notebook, "cells")))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
175 error ("jupyter_notebook: not valid format for Jupyter notebooks");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
176 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
177
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
178 ## Issue a warning if the format is lower than 4.0
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
179 if (obj.notebook.nbformat < 4)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
180 warning (["jupyter_notebook: nbformat versions lower than 4.0 are ", ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
181 "not supported"]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
182 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
183
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
184 ## Handle the case if there is only one cell.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
185 ## Make "obj.notebook.cells" a cell of structs to match the format.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
186 if (numel (obj.notebook.cells) == 1)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
187 obj.notebook.cells = {obj.notebook.cells};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
188 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
189
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
190 ## Handle the case if the cells have the same keys.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
191 ## Make "obj.notebook.cells" a cell of structs instead of struct array
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
192 ## to unify the indexing method.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
193 if (isstruct (obj.notebook.cells))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
194 obj.notebook.cells = num2cell (obj.notebook.cells);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
195 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
196
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
197 for i = 1:numel (obj.notebook.cells)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
198 if (! isfield (obj.notebook.cells{i}, "source"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
199 error ("jupyter_notebook: cells must contain a \"source\" field");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
200 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
201
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
202 if (! isfield (obj.notebook.cells{i}, "cell_type"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
203 error ("jupyter_notebook: cells must contain a \"cell_type\" field");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
204 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
205
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
206 ## Handle when null JSON values are decoded into empty arrays.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
207 if (isfield (obj.notebook.cells{i}, "execution_count")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
208 && numel (obj.notebook.cells{i}.execution_count) == 0)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
209 obj.notebook.cells{i}.execution_count = 1;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
210 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
211
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
212 ## Handle the case if there is only one output in the cell.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
213 ## Make the outputs of the cell a cell of structs to match the format.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
214 if (isfield (obj.notebook.cells{i}, "outputs")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
215 && numel (obj.notebook.cells{i}.outputs) == 1)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
216 obj.notebook.cells{i}.outputs = {obj.notebook.cells{i}.outputs};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
217 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
218 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
219
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
220 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
221
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
222
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
223 function generate_octave_script (obj, script_file_name)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
224
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
225 ## -*- texinfo -*-
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
226 ## @deftypefn {} {} generate_octave_script (@var{script_file_name})
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
227 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
228 ## Write an Octave script that has the contents of the Jupyter Notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
229 ## stored in the @qcode{notebook} attribute to @var{script_file_name}.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
230 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
231 ## Non code cells are generated as block comments.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
232 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
233 ## See @code{help jupyter_notebook} for examples.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
234 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
235 ## @end deftypefn
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
236
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
237 if (nargin != 2)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
238 print_usage ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
239 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
240
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
241 if (! (ischar (script_file_name) && isrow (script_file_name)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
242 error ("jupyter_notebook: script_file_name must be a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
243 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
244
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
245 fhandle = fopen (script_file_name, "w");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
246
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
247 for i = 1:numel (obj.notebook.cells)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
248 if (strcmp (obj.notebook.cells{i}.cell_type, "markdown"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
249 fputs (fhandle, "\n#{\n");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
250 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
251
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
252 for k = 1:numel (obj.notebook.cells{i}.source)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
253 fputs (fhandle, obj.notebook.cells{i}.source{k});
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
254 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
255
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
256 if (strcmp (obj.notebook.cells{i}.cell_type, "markdown"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
257 fputs (fhandle, "\n#}\n");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
258 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
259 fputs (fhandle, "\n");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
260 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
261 fclose (fhandle);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
262
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
263 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
264
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
265
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
266 function generate_notebook (obj, notebook_file_name)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
267
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
268 ## -*- texinfo -*-
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
269 ## @deftypefn {} {} generate_notebook (@var{notebook_file_name})
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
270 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
271 ## Write the Jupyter Notebook stored in the @qcode{notebook}
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
272 ## attribute to @var{notebook_file_name}.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
273 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
274 ## The @qcode{notebook} attribute is encoded to JSON text.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
275 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
276 ## See @code{help jupyter_notebook} for examples.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
277 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
278 ## @end deftypefn
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
279
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
280 if (nargin != 2)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
281 print_usage ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
282 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
283
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
284 if (! (ischar (notebook_file_name) && isrow (notebook_file_name)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
285 error ("jupyter_notebook: notebook_file_name must be a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
286 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
287
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
288 fhandle = fopen (notebook_file_name, "w");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
289
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
290 fputs (fhandle, jsonencode (obj.notebook, "ConvertInfAndNaN", false,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
291 "PrettyPrint", true));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
292
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
293 fclose (fhandle);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
294
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
295 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
296
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
297
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
298 function run (obj, cell_index)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
299
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
300 ## -*- texinfo -*-
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
301 ## @deftypefn {} {} run (@var{cell_index})
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
302 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
303 ## Run the Jupyter Notebook cell with index @var{cell_index}
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
304 ## and eventually replace previous output cells in the object.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
305 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
306 ## The first Jupyter Notebook cell has the index 1.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
307 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
308 ## Note: The code evaluation of the Jupyter Notebook cells is done
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
309 ## in a separate Jupyter Notebook context. Thus currently open
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
310 ## figures and workspace variables won't be affected by executing
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
311 ## this function. However, current workspace variables cannot be
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
312 ## accessed either.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
313 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
314 ## See @code{help jupyter_notebook} for examples.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
315 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
316 ## @end deftypefn
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
317
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
318 if (nargin != 2)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
319 print_usage ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
320 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
321
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
322 if (! (isscalar (cell_index) && ! islogical (cell_index)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
323 && (mod (cell_index, 1) == 0) && (cell_index > 0)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
324 error ("jupyter_notebook: cell_index must be a scalar positive integer");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
325 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
326
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
327 if (cell_index > length (obj.notebook.cells))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
328 error ("jupyter_notebook: cell_index is out of bound");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
329 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
330
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
331 if (! strcmp (obj.notebook.cells{cell_index}.cell_type, "code"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
332 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
333 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
334
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
335 ## Remove previous outputs.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
336 obj.notebook.cells{cell_index}.outputs = {};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
337
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
338 if (isempty (obj.notebook.cells{cell_index}.source))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
339 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
340 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
341
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
342 ## Default values for printOptions.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
343 printOptions.imageFormat = "png";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
344 printOptions.resolution = "0";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
345
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
346 ## The default width and height in Jupyter notebook
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
347 printOptions.width = "640";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
348 printOptions.height = "480";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
349
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
350 ## Parse "plot magic" commands.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
351 ## https://github.com/Calysto/metakernel/blob/master/metakernel/ ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
352 ## magics/README.md#plot
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
353 for j = 1 : numel (obj.notebook.cells{cell_index}.source)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
354 if (strncmpi (obj.notebook.cells{cell_index}.source{j}, "%plot", 5))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
355 magics = strsplit (strtrim (
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
356 obj.notebook.cells{cell_index}.source{j}));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
357 for i = 1 : numel (magics)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
358 if (any (strcmp (magics{i}, {"-f", "--format"}))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
359 && (i < numel (magics)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
360 printOptions.imageFormat = magics{i+1};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
361 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
362 if (any (strcmp (magics{i}, {"-r", "--resolution"}))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
363 && (i < numel (magics)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
364 printOptions.resolution = magics{i+1};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
365 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
366 if (any (strcmp (magics{i}, {"-w", "--width"}))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
367 && (i < numel (magics)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
368 printOptions.width = magics{i+1};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
369 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
370 if (any (strcmp (magics{i}, {"-h", "--height"}))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
371 && (i < numel (magics)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
372 printOptions.height = magics{i+1};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
373 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
374 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
375 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
376 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
377
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
378 ## Remember previously opened figures.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
379 fig_ids = findall (0, "type", "figure");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
380
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
381 ## Create a new figure, if there are existing plots.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
382 if (! isempty (fig_ids))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
383 newFig = figure ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
384 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
385
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
386 stream_output = struct ("name", "stdout", "output_type", "stream");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
387
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
388 output_lines = obj.evalCode (strjoin (
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
389 obj.notebook.cells{cell_index}.source));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
390
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
391 if (! isempty(output_lines))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
392 stream_output.text = {output_lines};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
393 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
394
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
395 if (isfield (stream_output, "text"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
396 obj.notebook.cells{cell_index}.outputs{end + 1} = stream_output;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
397 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
398
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
399 ## If there are existing plots and newFig is empty, delete it.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
400 if (exist ("newFig") && isempty (get (newFig, "children")))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
401 delete (newFig);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
402 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
403
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
404 ## Check for newly created figures.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
405 fig_ids_new = setdiff (findall (0, "type", "figure"), fig_ids);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
406
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
407 if (numel (fig_ids_new) > 0)
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
408 if (! isempty (obj.tmpdir) && exist (obj.tmpdir, "dir"))
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
409 ## Delete open figures before raising the error.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
410 for i = 1:numel (fig_ids_new)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
411 delete (fig_ids_new(i));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
412 endfor
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
413 error (["JupyterNotebook: temporary directory %s exists. ", ...
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
414 "Please remove it manually."], obj.tmpdir);
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
415 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
416
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
417 if (isempty (obj.tmpdir))
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
418 obj.tmpdir = tempname ();
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
419 clear_tmpdir_property = true;
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
420 else
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
421 clear_tmpdir_property = false;
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
422 endif
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
423 [status, msg, msgid] = mkdir (obj.tmpdir);
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
424 if (status == 0)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
425 ## Delete open figures before raising the error.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
426 for i = 1 : numel (fig_ids_new)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
427 delete (fig_ids_new(i));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
428 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
429 error (["jupyter_notebook: Cannot create a temporary directory. ", ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
430 msg]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
431 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
432
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
433 ## FIXME: Maybe it would be better for these cleanup actions to
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
434 ## happen in an onCleanup object or unwind_protect block so that
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
435 ## they will be executed no matter how we exit this function?
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
436
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
437 for i = 1:numel (fig_ids_new)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
438 figure (fig_ids_new(i), "visible", "off");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
439 obj.embedImage (cell_index, fig_ids_new (i), printOptions);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
440 delete (fig_ids_new(i));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
441 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
442
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
443 [status, msg, msgid] = rmdir (obj.tmpdir);
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
444 if (status == 0)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
445 error (["jupyter_notebook: Cannot delete the temporary ", ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
446 "directory. ", msg]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
447 endif
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
448 if (clear_tmpdir_property)
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
449 obj.tmpdir = "";
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
450 endif
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
451 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
452
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
453 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
454
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
455
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
456 function run_all (obj)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
457
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
458 ## -*- texinfo -*-
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
459 ## @deftypefn {} {} run_all ()
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
460 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
461 ## Run all Jupyter Notebook cells and eventually replace previous
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
462 ## output cells in the object.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
463 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
464 ## Note: The code evaluation of the Jupyter Notebook cells is done
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
465 ## in a separate Jupyter Notebook context. Thus currently open
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
466 ## figures and workspace variables won't be affected by executing
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
467 ## this function. However, current workspace variables cannot be
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
468 ## accessed either.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
469 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
470 ## See @code{help jupyter_notebook} for examples.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
471 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
472 ## @end deftypefn
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
473
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
474 if (nargin != 1)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
475 print_usage ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
476 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
477
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
478 for i = 1:numel (obj.notebook.cells)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
479 obj.run(i);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
480 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
481
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
482 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
483
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
484 endmethods
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
485
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
486
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
487 methods (Access = "private")
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
488
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
489 function retVal = evalCode (__obj__, __code__)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
490
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
491 ## Evaluate the code string "__code__" using "evalc".
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
492 ## Before the code is evaluated, the previous notebook context is
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
493 ## loaded from "__obj__" and the new context is saved to that struct.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
494
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
495 if (nargin != 2)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
496 print_usage ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
497 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
498
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
499 if (isempty (__code__))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
500 retVal = [];
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
501 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
502 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
503
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
504 if (! (ischar (__code__) && isrow (__code__)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
505 error ("jupyter_notebook: code must be a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
506 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
507
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
508 __obj__.loadContext ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
509
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
510 ## Add a statement to detect the value of the variable "ans"
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
511 __code__ = [__code__, "\nans"];
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
512
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
513 retVal = strtrim (evalc (__code__, ["printf (\"error: \"); ", ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
514 "printf (lasterror.message)"]));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
515
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
516 ## Handle the "ans" variable in the context.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
517 start_index = rindex (retVal, "ans =") + 6;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
518 if ((start_index > 6))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
519 if ((start_index <= length (retVal)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
520 end_index = start_index;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
521 while ((retVal(end_index) != "\n") && (end_index < length (retVal)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
522 end_index += 1;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
523 endwhile
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
524 __obj__.context.ans = retVal(start_index:end_index);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
525 else
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
526 end_index = length (retVal);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
527 __obj__.context.ans = "";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
528 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
529
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
530 ## Delete the output of the additional statement if the execution
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
531 ## is completed with no errors.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
532 if (end_index == length (retVal))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
533 ## Remove the extra new line if there are other outputs with
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
534 ## the "ans" statement output
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
535 if (start_index == 7)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
536 start_index = 1;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
537 else
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
538 start_index = start_index - 7;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
539 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
540 retVal(start_index:end_index) = "";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
541 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
542 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
543
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
544 __obj__.saveContext ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
545
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
546 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
547
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
548
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
549 function saveContext (obj, op)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
550
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
551 ## Save the context in private "obj" attribute.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
552
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
553 ## Handle the "ans" variable in the context.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
554 obj.context = struct ("ans", obj.context.ans);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
555
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
556 forbidden_var_names = {"__code__", "__obj__", "ans"};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
557
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
558 ## Get variable names.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
559 var_names = {evalin("caller", "whos").name};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
560
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
561 ## Store all variables to context.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
562 for i = 1:length (var_names)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
563 if (! any (strcmp (var_names{i}, forbidden_var_names)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
564 obj.context.(var_names{i}) = evalin ("caller", var_names{i});
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
565 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
566 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
567
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
568 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
569
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
570
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
571 function loadContext (obj)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
572
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
573 ## Load the context from private "obj" attribute.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
574 for [val, key] = obj.context
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
575 assignin ("caller", key, val);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
576 endfor
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
577
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
578 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
579
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
580
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
581 function embedImage (obj, cell_index, figHandle, printOptions)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
582
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
583 ## Embed images in the notebook.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
584 ##
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
585 ## To support a new format:
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
586 ## 1. Create a new function that embeds the new format
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
587 ## (e.g. embed_svg_image).
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
588 ## 2. Add a new case to the switch-statement below.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
589
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
590 if (isempty (get (figHandle, "children")))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
591 error_text = {"The figure is empty!"};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
592 obj.addErrorOutput (cell_index, "The figure is empty!");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
593 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
594 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
595
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
596 ## Check if the resolution is correct
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
597 if (isempty (str2num (printOptions.resolution)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
598 obj.addErrorOutput (cell_index,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
599 "A number is required for resolution, not a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
600 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
601 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
602
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
603 ## Check if the width is correct
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
604 if (isempty (str2num (printOptions.width)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
605 obj.addErrorOutput (cell_index,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
606 "A number is required for width, not a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
607 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
608 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
609
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
610 ## Check if the height is correct
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
611 if (isempty (str2num (printOptions.height)))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
612 obj.addErrorOutput (cell_index,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
613 "A number is required for height, not a string");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
614 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
615 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
616
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
617 switch (lower (printOptions.imageFormat))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
618 case "png"
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
619 display_output = obj.embed_png_jpg_image (figHandle,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
620 printOptions, "png");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
621 case "jpg"
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
622 display_output = obj.embed_png_jpg_image (figHandle,
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
623 printOptions, "jpg");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
624 case "svg"
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
625 display_output = obj.embed_svg_image (figHandle, printOptions);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
626 otherwise
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
627 obj.addErrorOutput (cell_index, ["Cannot embed the \'", ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
628 printOptions.imageFormat, ...
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
629 "\' image format\n"]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
630 return;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
631 endswitch
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
632
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
633 obj.notebook.cells{cell_index}.outputs{end + 1} = display_output;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
634
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
635 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
636
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
637
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
638 function dstruct = embed_png_jpg_image (obj, figHandle, printOptions, fmt)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
639
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
640 if (strcmp (fmt, "png"))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
641 mime = "image/png";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
642 else
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
643 mime = "image/jpeg";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
644 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
645
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
646 image_path = fullfile (obj.tmpdir, ["temp.", fmt]);
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
647 print (figHandle, image_path, ["-d", fmt],
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
648 ["-r" printOptions.resolution]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
649
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
650 dstruct.output_type = "display_data";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
651 dstruct.metadata.(mime).width = printOptions.width;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
652 dstruct.metadata.(mime).height = printOptions.height;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
653 dstruct.data.("text/plain") = {"<IPython.core.display.Image object>"};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
654 dstruct.data.(mime) = base64_encode (uint8 (fileread (image_path)));
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
655
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
656 delete (image_path);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
657
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
658 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
659
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
660
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
661 function dstruct = embed_svg_image (obj, figHandle, printOptions)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
662
30294
488548c762de jupyter_notebook: don't hardwire tmp directory name (bug #61470)
John W. Eaton <jwe@octave.org>
parents: 30293
diff changeset
663 image_path = fullfile (obj.tmpdir, "temp.svg");
30293
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
664 print (figHandle, image_path, "-dsvg", ["-r" printOptions.resolution]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
665
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
666 dstruct.output_type = "display_data";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
667 dstruct.metadata = struct ();
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
668 dstruct.data.("text/plain") = {"<IPython.core.display.SVG object>"};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
669 dstruct.data.("image/svg+xml") = strsplit (fileread (image_path), "\n");
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
670
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
671 ## FIXME: The following is a workaround until we can properly print
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
672 ## SVG images in the right width and height.
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
673 ## Detect the <svg> tag. it is either the first or the second item
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
674 if (strncmpi (dstruct.data.("image/svg+xml"){1}, "<svg", 4))
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
675 i = 1;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
676 else
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
677 i = 2;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
678 endif
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
679
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
680 ## Embed the width and height in the image itself
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
681 svg_tag = dstruct.data.("image/svg+xml"){i};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
682 svg_tag = regexprep (svg_tag, "width=\"(.*?)\"",
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
683 ["width=\"" printOptions.width "px\""]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
684 svg_tag = regexprep (svg_tag, "height=\"(.*?)\"",
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
685 ["height=\"" printOptions.height "px\""]);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
686 dstruct.data.("image/svg+xml"){i} = svg_tag;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
687
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
688 delete (image_path);
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
689
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
690 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
691
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
692
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
693 function addErrorOutput (obj, cell_index, error_msg)
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
694
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
695 stream_output.name = "stderr";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
696 stream_output.output_type = "stream";
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
697 stream_output.text = {error_msg};
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
698 obj.notebook.cells{cell_index}.outputs{end + 1} = stream_output;
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
699
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
700 endfunction
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
701
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
702 endmethods
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
703
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
704 endclassdef
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
705
1a1f3ae76e74 Use underscores instead of CamelCase in jupyter_notebook classdef
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
706 #!error jupyter_notebook ()