Filipe Saraiva's blog

Tecnologia, sociedade e política.

Cantor: Python 2 backend feature tour

with 12 comments

Introduction

python2_selectCantor backend selection screen

In 2013 I developed a Python 2 backend for Cantor, a project funded in part by Google Summer of Code. Now this backend is available in Cantor released in KDE 4.12.

Cantor is a mathematical/scientific programming software, a frontend providing IDE features (syntax highlighting, tab-complete, variables management, and more) and a advanced terminal. Cantor support a lot of mathematical engines like Octave, Sage, Maxima, Kalgebra, Qualculate, R, Scilab (developed by me too), and now, Python 2. You can see Cantor as a Matlab-like software, but it uses other mathematical environment/software as programming language.

This post I will do a “feature tour” in Python 2 backend to show Cantor software for scientific python developers community.

Initial Screen – Syntax Highlighting and Tab Complete

python2_initialscreenCantor initial screen

After select the Python 2 backend, Cantor will show the initial screen. This window have a big widget for the Python 2 terminal, and two side panels – one to show the Python 2 help and the other to variables management.

Let’s see some commands inputs in the terminal:

python2_syntaxSyntax highlithing

Cantor is highlighting the Python 2 syntax and, in side panel, you can see the variables created.

Now, let’s create new variables with similar names to test the tab complete. See the picture below:

python2_completeTab complete for variables

After create variables variable_x, variable_y e variable_var, we can write in terminal var and type tab key twice – Cantor will show the variables and functions with the same piece of word. Tab complete is available for module functions too.

python2_complete2Tab complete for module functions

Cantor show error messages in terminal too. Next figure show a import error:

python2_errorError message

You can save the terminal state or just the input commands and their outputs in a file. Cantor allows upload/download a terminal example for a remote server. You can explore this features in “File” menu.

Help Panel

Cantor shows Python help from help command in a side panel. The picture below shows the help for complex class:

python2_helpHelp panel

Cantor uses Qt/KDE technologies, so you can change the window format moving the side panels. Next picture show the window with the variables management in the left side and the help panel in the right side.

python2_help_completePanels in different sides

Variables Management

python2_variables Variables management panel

Variables management panel shows the variables created in Cantor session, showing their labels and values. The panel have some additional functions too, in buttons bottom the widget. These functions are, from left to right, Add variableLoad variables, Save variables, and Clear all variables.

Add variable just open a pop-up window to input a label and a value to a variable.

Load variables and Save variables uses shelve module to data persistence. When the buttons are clicked, Cantor loads scripts to, in first case, read and load variables to the session, and, in second case, save the python dictionary in a file. The figures below show this operations:

python2_save_variablesSaving variables

python2_load_variablesLoading variables

The function Clear all variables delete each variable from Python dictionary. The code is below:

python_backend_variable_management_usecase6Graphics

python_command_to_plotCode for graphic creation using matplotlib

python_plot_resultGraphic loaded in Cantor

The figures below show the graphic loaded in Cantor worksheet. When a session is exported, the graphic will be exported too. This feature can be configured to create the graphics in a new window – this is the default option.

Conclusion and Future

This is the first stable version of Python 2 support in Cantor. It is working good, but you can see the bug presence in some parts of software.

I would like to see some feedbacks from mathematical/scientific programming python community. I am not a “pythonist”, so the python community can find bugs and strange behaviours in the software better than me. I would like to see some feature suggestions too.

For the next version, Python 2 backend will have support to script editor – unfortunately, I can not develop it for this release.

If you are interested in the backend development, my blog have a set of posts (in Portuguese and English) about it. You can download the source code in Cantor repository – the license is GPLv2. And you can submit a bug report in KDE bugzilla.

And you can contact me in comments area below or in my mail address filipe at kde.org.

Written by Filipe Saraiva

January 16th, 2014 at 10:42 am

12 Responses to “Cantor: Python 2 backend feature tour”

  1. Tuukka says:

    This is pretty cool. I’ve been looking forward to having a Python backend. However, it didn’t really work for me because of a problem with importing numpy. I submitted a bug report.

  2. Victor says:

    `del` is a statement, not built in function. So no need to write `del(KeyPythonBackend)`, write `del KeyPythonBackend`

  3. anonymity is great says:

    Your work looks great! I am not a Pythonista either, so I cannot thoroughly test this. One remark though: the “Help” panel should show the documentation in HTML format. Plain text format in a GUI is so 1980s. If there is no local documentation in HTML format, you can show the online version and when the online version doesn’t load (because of no internet connection for example), you can show the text version instead with a warning that the online version could not be loaded.

    • Hi anonymity, I need first to see if help command returns a HTML output. But thanks for your feedback, I will see what I can do.

      • TheBlackCat says:

        I don’t think it does, but as I said below, there at tools to do the conversion. For example, Sphinx is a common one, but I am not sure if it is fast enough for these purposes.

  4. TheBlackCat says:

    Looking nice so far.

    A few suggestions:

    1. You use “traditional” division, which is really broken for anyone trying to do mathematics. You get stuff like 1/2 = 0. You should do “from __future__ import division” to fix this. That make 1/2 = .5.

    2. sympy offers pretty-printing for latex-enabled renderers. You should look into using that to get prettier output when latex is enabled.

    3. There is a built-in pretty-printer module called pprint. I might suggest passing every output through that before displaying it.

    4. There are reStructured text interpreters for python docstrings. I might suggest passing your help output through that if you can it quickly enough. It produces much nicer displays.

    5. When displaying lists, it is common in python to diplay the begining and end of the list, with … in-between, rather than just the beginnning us you do. It would probably also be useful to display the dimensions.

    6. It would probably be good, as an option at least, to import numpy if not pylab into the global namespace, or at least “import numpy as np”.

    7. Users should be able to specify modules to import at startup.

    8. Variables imported from modules, including functions and classes, such as “from numpy import *”, are displayed in the variable manager. This shouldn’t happen. Only user-defined variables should be displayed. Having a seperate panel for available modules is probably a good idea.

    9. There should be an auto-indenter for things like functions, if tests, for loops, etc.

    10. The result of “print” statements or functions is displayed identically to the returned value of an operation. There should probably be something to visually differentiate them. Also, if the returned value of a function is None, it probably should not be displayed at all.

    11. It would be nice if there was a way to open a list or array in full to edit it element-by-element.

    12. storing an iterator to a variable gives “listiterator object at 0xblahblah”. We probably don’t need to now the “at 0x___” stuff.

    13. Is there a way to make it scroll inside results that are too large? Try running:
    ‘\n’.join(['*'*i for i in xrange(100)])

    • Hi TheBlackCat, thanks for your loooooong comment and sorry for my delay.

      I tried to reply some of comments below:

      >>> 1. You use “traditional” division, which is really broken for anyone trying to do mathematics. You get stuff like 1/2 = 0. You should do “from __future__ import division” to fix this. That make 1/2 = .5.

      Oh god, it is crazy! Thanks, I will to code a solution to load this when Python2 backend be chosen. I see Python 3 don’t have this problem.

      >>> 2. sympy offers pretty-printing for latex-enabled renderers. You should look into using that to get prettier output when latex is enabled.

      I didn’t developed LaTeX support, but it is a feature to next release.

      >>> 3. There is a built-in pretty-printer module called pprint. I might suggest passing every output through that before displaying it.

      I will verify it. I prefer to use a minimal set of modules, but if pprint is a really great addition to backend, I will think in their inclusion.

      >>> 4. There are reStructured text interpreters for python docstrings. I might suggest passing your help output through that if you can it quickly enough. It produces much nicer displays.

      I need improve help panel. I will verify your tip.

      >>> 5. When displaying lists, it is common in python to diplay the begining and end of the list, with … in-between, rather than just the beginnning us you do. It would probably also be useful to display the dimensions.

      I really need to improve variable management.

      >>> 6. It would probably be good, as an option at least, to import numpy if not pylab into the global namespace, or at least “import numpy as np”.

      I don’t understand your question. “import numpy as np” is supported.

      >>> 7. Users should be able to specify modules to import at startup.

      You talked about this in a comment in an old post. I need to see how develop it, maybe as a plugin in Cantor.

      >>> 8. Variables imported from modules, including functions and classes, such as “from numpy import *”, are displayed in the variable manager. This shouldn’t happen. Only user-defined variables should be displayed. Having a seperate panel for available modules is probably a good idea.

      I really need to improve variable management. =(

      >>> 9. There should be an auto-indenter for things like functions, if tests, for loops, etc.

      It is a problem with Cantor. All backends as this problem. I will talk with the other developers to know if we can solve it.

      >>> 10. The result of “print” statements or functions is displayed identically to the returned value of an operation. There should probably be something to visually differentiate them. Also, if the returned value of a function is None, it probably should not be displayed at all.

      It is a problem, really, but for now it is not prooritary for next backend version.

      >>> 11. It would be nice if there was a way to open a list or array in full to edit it element-by-element.

      Maybe a solution to all backends in Cantor would be nice.

      >>> 12. storing an iterator to a variable gives “listiterator object at 0xblahblah”. We probably don’t need to now the “at 0x___” stuff.

      Variable management will be improve, I promise. o/

      >>> 13. Is there a way to make it scroll inside results that are too large? Try running:
      ‘\n’.join(['*'*i for i in xrange(100)])

      I don’t understand the command. Could you paste the complete command?

      Thanks for your feedbacks! It was really appreciate.

      • TheBlackCat says:

        >>>>>> 3. There is a built-in pretty-printer module called pprint. I might suggest passing every output through that before displaying it.

        >>> I will verify it. I prefer to use a minimal set of modules, but if pprint is a really great addition to backend, I will think in their inclusion.

        It is part of the python standard library, so it won’t add any additional dependencies.

        >>>>>> 6. It would probably be good, as an option at least, to import numpy if not pylab into the global namespace, or at least “import numpy as np”.

        >>> I don’t understand your question. “import numpy as np” is supported.

        True, but I think it should probably be available, at least as an option, to have it loaded into the workspace automatically.

        >>>>>> 13. Is there a way to make it scroll inside results that are too large? Try running:
        ‘\n’.join(['*'*i for i in xrange(100)])

        >>> I don’t understand the command. Could you paste the complete command?

        That is the complete command. Just paste it in to the command line and run it (it seems the quotes might get converted by the blog software, make sure they are ordinary quotes):

        ‘\n’.join(['*'*i for i in xrange(100)])

  5. Troy Unrau says:

    This is fantastic! I use python for numerical work quite frequently, and could see myself using this. Any chance it works on Windows?

Leave a Reply