Python Backend for Cantor: Append Plot Images in Cantor Workspace

Other feature implemented in python backend for Cantor in last weeks was “append plot image to Cantor Workspace”.

In other backends you can, optionally, generate a plot image and this image will be append in Cantor workspace, not generating a separated window to the picture.

Below we have a command to generate a plot image in python using matplotlib and pyplot:

python_command_to_plot

Now we have the result appended in Cantor workspace:

python_plot_resultIn python, to save a picture using pyplot, we type the command pyplot.savefig(). But, if a picture was saved, it can not be shown in separated window. Otherwise, if a picture is shown in a separated window, it can not be saved to a file.

To solve this problem, the python backend change the show() command to savefig(), with a random name to the picture. The image is saved in a temporary file and loaded in Cantor workspace.

The option to load figure in Cantor workspace or to use a separated window is configured in python backend configuration screen. The default is to use separated window because matplotlib/pyplot have several additional features in image screen.

I would like to see some feedback from you, in special if you are a python developer. The code is hosted in python-backend branch from Cantor repository.

Side panels in Python Backend for Cantor: Help and Variable Management

In last weeks I developed some new features to python backend for Cantor. In this post I will write about two of their: the help and the variable management, implemented as panels.

python_backend_help_panel python_backend_variable_management

 Help Panel on left; Variable Management Panel on right

Help panel

Help panel shows the help output in a separated panel, facilitating the consult of this information. To use it, simply use help command as in python interactive mode, as in example below:

python_backend_help_panel_usecase_1

Below a bigger help output, from a python module:

 python_backend_help_panel_usecase_2

In previous picture, did you see some change in variable management panel?

Variable Management panel

Variable management panel is a great feature provide by Cantor, but until now just Octave backend had it. This feature show the variables defined in the session, their values, and allow some interesting functions to manipulate these variables.

You can define a lot of variables in python session and these variables will be shown in the panel. See:

python_backend_variable_management_usecase1

In previous picture, I defined a integer variables x and y, a string a, and two modules: numpy and scipy, this last as sc. All this values are shown in panel.

Now I will change some values defined previously and will add some others:

python_backend_variable_management_usecase3

See, now I defined a variable aa by the concatenation of two a‘s; the value of a is now aa; div is the division of y by x. I have a 2-dimensional matrix mtr defined by matrix function from numpy module; a python class HelloWorldClass and a object hello.

I can change the value of some these variable manipulating their values in the panel. For example, I can rewrite the x variable from 35 to 350, clicking in value column and typing 350.

But the more interesting features are provide by the buttons bellow this panel. These buttons load python scripts to run some feature. There are: add variable, load variables from a file, save variables to a file and clear all variables in the session.

Add variable is quite simple: a dialog is open and you type the variable and their value:

python_backend_variable_management_usecase4Save/Load use shelve module to save and load the variables of the session. But, it is important to say, this feature don’t save all the variables because shelve module have some limitations. For example, I saved this session to a file named “python_session.txt”. The python code loaded is:

python_backend_variable_management_usecase5

After save, I will clear the session. The python code loaded is:

python_backend_variable_management_usecase6And, finally, I will load the variables. The python code loaded ans the variable panel is show below:

python_backend_variable_management_usecase8

Conclusions

It is working now but I need some more tests, in special the save/load python objects defined by user and modules.

I would like to see some feedback from you. The code is hosted in python-backend branch from Cantor repository, so you can test it.

Cantor – palavras-chave dinâmicas no python backend

No post anterior, eu mencionei sobre palavras-chave dinâmicas no python backend. A ideia é que, após importar um módulo python na área de trabalho do Cantor, funções, palavras-chave, variáveis e mais deste módulo são carredados no Cantor e disponibilizados para o destaque de sintaxe e tab complete.

Esta funcionalidade já está implementada e você pode testá-la compilando o Cantor do branch python-backend.

Mas vamos antes ver mais algumas informações sobre esta funcionalidade.

Existem várias maneiras de importar um módulo python no console python. Você pode usar “import modulename”, “import modulename as modulevariable”, “from modulename import *”, “from modulename import function_1, function_2, …”, e mais. Cada maneira de fazer um import provoca diferentes consequências na experiência do usuário.

As quatro maneiras de realizar um import mencionadas anteriormente são suportadas pelo python backend. Veremos agora como esses diferentes imports se comportam e como o python backend reage a cada um deles.

import modulename

A maneira mais básica e simples de fazer um import. Após este comando, uma variável chamada “modulename” é definida e as funções e mais palavras-chave do módulo são disponibilizadas através de “modulename.keyword”.

python_import_modulenameTab Complete

python_import_modulename_highlighting

Destaque de Sintaxe

import modulename as modulevariable

Nesta forma o usuário define uma variável “modulevariable” para referenciar o “modulename”, e “modulename” não é definido. Portanto, você pode acessar as funções e mais palavras-chave do “modulename” usando “modulevariable.keyword”.

python_import_modulename_as_modulevariable

Tab Complete

python_import_modulename_as_modulevariable_highlithing

Destaque de Sintaxe

from modulename import *

Nesta forma o usuário importa todas as funções e palavras-chave do “modulename”, mas nenhuma variável é definida para acessar o “modulename”. As funções do módulo são acessadas diretamente.

python_from_modulename_import_all

Tab Complete

python_from_modulename_import_all_highlithing

Destaque de Sintaxe

from modulename import function_1, function_2, …

O usuário importa apenas funções específicas do “modulename”, e não todas as funções.

python_from_modulename_import_specific

Tab Complete

python_from_modulename_import_specific_highlithing

Destaque de Sintaxe

Cantor plugin

Eu também desenvolvi um plugin para importar módulos no Cantor. Este plugin abre uma janela de diálogo para o usuário entrar com o módulo e, após apertar o Ok, Cantor executará um “import modulename” e as palavras-chave estarão disponíveis. O diálogo é acessado via o menu “Packaging”, na barra de menu.

python_import_cantor_plugin

Plugin de import no Cantor

Identificando erros

O backend pode identificar vários tipos de erros durante o import.

python_import_errors

Identificando erros no import

Conclusões

A funcionalidade está desenvolvida e madura para uso, entretanto, ela não suporta todas a maneiras de realizar um import em python. Entretanto, imagino que estas cinco maneiras de realizar o import representam as formas mais comuns utilizadas por desenvolvedores python com foco em softwares científicos.

O importante é que esta funcionalidade permite ao python backend suportar os mais variados tipos de módulos python, e não somente scipy, numpy, and matplotlib, como eu havia proposto no início desse projeto.

Agora, deixe-me saber como você importa módulos no python. Eu desenvolverei o suporte para novas formas de import em futuras versões do backend.

Por enquanto, espere por mais novidades deste projeto logo mais!

Cantor – dynamic keywords in python backend

In previous post, I mentioned about dynamic keywords in python backend. The idea is, after import a python module in Cantor workspace, functions, keywords, variables, and more from this module are load by Cantor and available to syntax highlighting and tab complete.

This feature is implemented for now. You can test it compiling Cantor from python-backend branch.

But, let me show more information about this feature.

There are several ways to import a python module in python console. You have “import modulename”, “import modulename as modulevariable”, “from modulename import *”, “from modulename import function_1, function_2, …”, and more. Each import way causes different consequences to user experience.

The four import ways mentioned in previous paragraph are supported by python backend. I will show these different import ways and how python backend behave for each one.

import modulename

The more basic import way. After this command, a variable named “modulename” is defined and the functions and more keywords of this module are available to access using “modulename.keyword”.

python_import_modulenameTab Complete

python_import_modulename_highlighting

Syntax Highlighting

import modulename as modulevariable

This way the user define a name “modulevariable” to reference “modulename”, and “modulename” is not defined. So, you can access the functions and more from “modulename” using “modulevariable.keyword”.

python_import_modulename_as_modulevariable

Tab Complete

python_import_modulename_as_modulevariable_highlithing

Syntax Highlighting

from modulename import *

This way the user import all functions and keywords from “modulename” but anything variable is defined to access “modulename”. The functions of the module are accessed directly.

python_from_modulename_import_all

Tab Complete

python_from_modulename_import_all_highlithing

Syntax Highlighting

from modulename import function_1, function_2, …

The user import only specific functions from a “modulename”, no all functions.

python_from_modulename_import_specific

Tab Complete

python_from_modulename_import_specific_highlithing

Syntax Highlighting

Cantor plugin

I developed a Cantor plugin to import modules. This plugin open a dialog to user enter a modulename and, after press Ok, Cantor run “import modulename” and keywords are available. The diaglog is accessible by “Packaging” menu, in toolbar.

python_import_cantor_plugin

Cantor Plugin

Handling Errors

The backend can identify several errors during import.

python_import_errors

Handling Errors

Conclusions

Well, the feature is working and it is mature for use, however it don’t support all import ways in python. But, I think these five ways cover the most commons import ways used by most python scientific users.

The important thing is, this feature enable python backend to support the several python modules, and no only scipy, numpy, and matplotlib, as I proposed in begin of this project.

Let me know how you import a module in python. I will develop support to more import ways in future versions of the backend.

For now, wait for more news of this project soon!

Backend Python para Cantor – Destaque de Sintaxe, Tab Complete, e Modo Interactive

Meu projeto no Google Summer of Code 2013, o desenvolvimento de um backend para python no Cantor, atingiu alguns objetivos nas últimas semanas.

Primeiramente eu desenvolvi a versão preliminar para as funcionalidades de destaque de sintaxe e tab complete. Esta etapa utilizou processamento de arquivos XML para prover um conjunto básico de palavras-chave e funções predefinidas para aquelas funcionalidades. Para ativar o tab complete, basta pressionar o botão Tav após ter digitado alguma letra ou conjunto de letras. Abaixo temos alguns screenshots destas funcionalidades (clique com o botão direito do mouse para ampliar as imagens):

python_syntax_highlighting

Um simples destaque de sintaxe

python_backend

Mais destaque de sintaxe e tab complete

Em seguida, a versão inicial do backend desenvolvido durante o LaKademy foi criada para dar suporte ao modo de script do python. Mas seria mais interessante dar suporte ao modo interactive no terminal do Cantor. Então, para fazer isso, eu implementei um pré-processamento nos códigos do python que o usuário insere no Cantor antes de enviá-los ao interpretador python, adicionando alguns comandos para simular o modo interactive do python.

Esta solução utiliza o PyRun_SimpleString da API python/C para processar os comandos python em modo de script. Existem outras funções da API python/C para processar os comandos python diretamente em modo interactive, por exemplo, PyRun_InteractiveOne, mas esta função requer que os comandos python a serem processados estejam em um arquivo, além do que acessar a saída desse processamento é algo bastante difícil. Então, para o momento,  PyRun_SimpleString + pré-processamento dos comandos é uma solução que está funcionando muito bem.

Você pode ver nas screenshots que ilustram este post que as variáveis e resultados de operações apresentados na tela do Cantor não usam o comando print. Esta é uma das características do modo interactive do python:

python_backend_aninhado1Mais exemplos de destaque de sintaxe e interactive mode

python_backend_aninhado2

Mais exemplos de destaque de sintaxe e interactive mode

Agora eu estou desenvolvendo uma funcionalidade para listar todas as funções predefinidas e palavras-chave de um módulo python específico que tenha sido importado durante a execução do programa. Para tanto, eu estou usando o comando dir do python para capturar esses dados. Esta funcionalidade proverá estas palavras para o destaque de sintaxe e o tab complete, de uma forma dinâmica. Assim, o backend para python dará suporte a qualquer módulo python, e não mais apenas ao scipy, numpy, e matplotlib.

O backend para python está disponível no branch python-backend no repositório do Cantor. Fique a vontade para testá-lo, mas lembre-se que o código não está estável e finalizado no momento.

Python backend for Cantor – Syntax Highlighting, Tab Complete, and Interactive Mode

My GSoC’2013 project for development a python backend for Cantor reach some objectives in before weeks.

Firstly I developed preliminary versions of syntax highlighting and tab complete features. This version use XML file processing to provide a basic set of keywords and built-in functions to these features. To activate tab complete, press Tab button after some letter.  You can see some screenshots of these features below (click mouse right-button and enlarge it):

python_syntax_highlighting

A simple syntax highlighting

python_backend

More syntax highlighting and tab complete

Next, the preliminary version  of the backend developed during LaKademy was build to support python script mode. But it would be more interesting to support python interactive mode in Cantor console. So, to do it, I implemented preprocessing in python command before send it to python interpreter, adding some commands to simulate python interactive mode.

This solution use PyRun_SimpleString from python/C API to process python commands in python script mode. There is other functions from python/C API to process python commands in interactive mode, for example, PyRun_InteractiveOne, but this function requires the python commands in a file to process and the output of commands is a bit of hard to get. So, for now, PyRun_SimpleString + preprocessing commands is working very well.

You can see in screenshots below and above that variable values and operation values are show in Cantor console without use of print command. It is a one of characteristic of python interactive mode:

python_backend_aninhado1More examples of syntax highlighting and python interactive mode

python_backend_aninhado2

More examples of syntax highlighting and python interactive mode

Now I am developing a feature to list all functions and keywords from a specific python module imported during the program execution. I am using dir command to do it. This feature will provide these keywords to use in syntax highlighting and tab complete in a dynamic way. Then, python backend will support any python module, and not scipy, numpy, and matplotlib only.

Python backend is available in python-backend branch in Cantor repository. You can test it, but remember this code is not stable and finalized for now.

Google Summer of Code 2013: Cantor + Python

This year I have a project accepted to Google Summer of Code. This is my second time in the program and I am very happy and grateful for it

This year I come back to Cantor, the KDE mathematical environment for several mathematical softwares like Maxima, Sage, Scilab, etc. In 2011 I developed the Scilab backend during Google Summer of Code. My mentor was Sylvestre Ledru, from Scilab team.

This time my project is conclude the scientific python backend for Cantor. I began the development of this piece of software during LaKademy. The communication between Cantor and python is working properly. The software miss some features of IDE, like tab-complete and syntax highlight. I intent provide support to scipy, numpy and matplotlib too. Alexander Rieder, from KDE, is my supervisor.

There are some ideas about how to implement tab-complete and syntax highlight. It is possible using XML files (like I implemented in Scilab backend) or use the Kdevelop technology. I am researching which I will use.

More updates will come soon! Follow the tag gsoc2013-python-backend this blog!