-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.do.txt
238 lines (181 loc) · 3.81 KB
/
execute.do.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
========= Automatic execution of code blocks =========
Convert this document to `ipynb`, `latex` or `html` with e.g.:
!bc sh-t
doconce format ipynb execute.do.txt --execute
!ec
======= Code blocks in different languages =======
===== Python =====
Python code
!bc py
for i in [1,2,3]:
print(i)
!ec
!bc py
print(i)
!ec
===== Bash =====
Bash code
!bc sh
if [ 1 -eq 1 ] ; then echo 1; fi
var_bash=10
!ec
!bc sh
echo $var_bash
!ec
===== Julia =====
Julia code
!bc jl
var_julia = 11
print(var_julia)
!ec
!bc jl
for n = 2:4
var_julia = var_julia + n
end
print(var_julia)
!ec
===== R =====
R code
!bc r
x <- 1:3
print(x)
#pdf("plot.pdf")
plot(x)
#dev.off()
!ec
===== Other languages =====
Then Cython (with -h option so it is hidden in html/sphinx):
!bc cycod-h
cpdef f(double x):
return x + 1
!ec
Java code
!bc java
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
!ec
Javascript code
!bc js
for (var x in [0,1,2]) {console.log(x)}
!ec
matlab code
!bc
for i = 1:2:10
disp(A(i))
end
!ec
html code
!bc html
<a href='test'></a>
!ec
C code
!bc c
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
!ec
======= Code block environments =======
Hidden execution cells (`pyhid`, `pycod-e`) can be used to perform operations (e.g. imports, variable initializations) without showing any cell.
The `pyhid` environment executes and hides the cell in formats other than .ipynb:
This is a normal python block using the `pycod` environment
!bc pycod
print('pycod')
!ec
!bc pyhid
# This cell appears in ipynb but not in other formats
import sys
print('hide pyhyd')
!ec
The `pycod-e` environment executes but hides the cell also in .ipynb files:
!bc pycod-e
import os
os.listdir(".")
a = 1
print('hide pycod-e')
!ec
`pycod` is a normal cell that should execute automatically when using `--execute`. Note that this cells relies on code executed in a previous hidden cell:
!bc pycod
print(sys.version)
b = 2
c = a + b
print("The result is {}".format(c))
c
!ec
!split
======= Special environments =======
The `*-t` environment (e.g. `pycod-t`) formats a cell to text, and can be used to print an example
!bc pycod-t
# This is a for-loop example
for i in [0,10]:
print(i)
!ec
The `*out` (e.g. `pycod-out`) environment can be used to write a cell output:
!bc pycod-t
# This is a text cell using pycod-t
1/0
!ec
!bc pyout
# This is a output cell using the `pycod-out` environment
1/0: You cannot divide by zero
!ec
The `-h` postfix can be used in the `html` format to show a Show/Hide button that toggles the code visibility.
% if FORMAT in ('html'):
!bc pycod-h
print('show/hide')
!ec
% endif
The `pyscpro` environment creates an interactive cell using "Sage": "https://github.com/sagemath/sagecell/" in the `html` format
% if FORMAT in ('html'):
!bc pyscpro
print('Execute this cell in Sage')
!ec
% endif
!split
======= Plotting =======
This is a cell that should plot and output:
!bc pycod
from pylab import *
x = linspace(0, 10, 100)
plot(x, x*x)
show()
!ec
To improve quality when exporting to LaTeX, the following code has automatically
been run to enable PDF export in notebooks.
!bc pycod-t
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
!ec
!split
======= Ignore output =======
Predefined output can be omitted by passing `--ignore_output` to DocOnce.
This will remove all environments ending with `out`.
!bc pycod
a = 2
print(a)
!ec
!bc pyout
2
!ec
!split
======= Code with errors =======
If code contains errors, it will still be run and the exception shown as part
of the output:
!bc pycod
for a in range(10)
print(a)
!ec
!split
======= Opening files =======
The working directory is the same as the .do.txt file.
You may want to use `os.chdir` to change the directory.
!bc pycod
with open("../LICENSE") as f:
print(f.read())
!ec