28

When you run from the command line

$ cython -a mycode.pyx

you get a really nice HTML "annotation" file with yellow shading to indicate slow python operations vs fast C operations. You also get this same HTML file as a link every time you compile Cython code in Sage. My questions are: (1) Can I get this HTML file if I'm compiling using distutils? (2) Can I get this HTML file if I'm compiling using pyximport? Thanks!!

Steve Byrnes
  • 2,210
  • 1
  • 20
  • 25
  • You can always call the `cython` command using `subprocess`. Otherwise, read the Cython source code; it's not tremendously hard to follow. – Fred Foo Jun 16 '12 at 14:29

1 Answers1

36

Thanks to larsmans's comment and the Cython email list, I now have many satisfying options to generate the "annotate" HTML file without leaving IPython:

(1) Use subprocess...

import subprocess
subprocess.call(["cython","-a","myfilename.pyx"])

(2) Turn on the global annotate flag in Cython myself, before compiling:

import Cython.Compiler.Options
Cython.Compiler.Options.annotate = True

(3) Pass annotate=True into cythonize() [when using the distutils compilation method].

It seems that pyximport does not have its own direct option for turning on annotation.

Steve Byrnes
  • 2,210
  • 1
  • 20
  • 25
  • As one might guess, it's `cython3` for Python 3. – handle Nov 19 '17 at 11:42
  • `annotate` is not to be found in http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiler-directives and adding it as `#cython: annotate=True` does not generate the file, so I just used your option 3 (successfully). – handle Nov 20 '17 at 18:40
  • @handle - Option (2) has always been undocumented. Did you actually try it? – Steve Byrnes Nov 20 '17 at 21:13
  • Not sure what you mean. I tried the "magic" compiler directive assuming this way might work also. I did not try setting an attribute as in (2). (3) works. – handle Nov 20 '17 at 21:22
  • 1
    All the three options can work if it doesn't work, please delete `.c`, `.so` and `.o` files and rerun `python setup.py build_ext --inplace ` to compile it. Your answer is really helpful. – GoingMyWay Jul 23 '18 at 08:53