Convert this document to ipynb
, latex
or html
with e.g.:
doconce format ipynb execute.do.txt --execute
Python code
for i in [1,2,3]:
print(i)
print(i)
Bash code
if [ 1 -eq 1 ] ; then echo 1; fi
var_bash=10
echo $var_bash
Julia code
var_julia = 11
print(var_julia)
for n = 2:4
var_julia = var_julia + n
end
print(var_julia)
R code
x <- 1:3
print(x)
#pdf("plot.pdf")
plot(x)
#dev.off()
Then Cython (with -h option so it is hidden in html/sphinx):
Java code
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Javascript code
for (var x in [0,1,2]) {console.log(x)}
matlab code
for i = 1:2:10
disp(A(i))
end
html code
<a href='test'></a>
C code
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
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
print('pycod')
The pycod-e
environment executes but hides the cell also in .ipynb files:
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:
print(sys.version)
b = 2
c = a + b
print("The result is {}".format(c))
c
The *-t
environment (e.g. pycod-t
) formats a cell to text, and can be used to print an example
# This is a for-loop example
for i in [0,10]:
print(i)
The *out
(e.g. pycod-out
) environment can be used to write a cell output:
# This is a text cell using pycod-t
1/0
The -h
postfix can be used in the html
format to show a Show/Hide button that toggles the code visibility.
The pyscpro
environment creates an interactive cell using Sage in the html
format
This is a cell that should plot and output:
from pylab import *
x = linspace(0, 10, 100)
plot(x, x*x)
show()
We can now refer to this plot as Figure python_plot.
To improve quality when exporting to LaTeX, the following code has automatically been run to enable PDF export in notebooks.
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
Predefined output can be omitted by passing --ignore_output
to DocOnce.
This will remove all environments ending with out
.
a = 2
print(a)
If code contains errors, it will still be run and the exception shown as part of the output:
for a in range(10)
print(a)
The working directory is the same as the .do.txt file.
You may want to use os.chdir
to change the directory.
with open("../LICENSE") as f:
print(f.read())