Python Annotation Improvements

I think the python annotation could be improved to be more generic and versatile.

There is a few axes of improvements :

  • Let the user import any python modules
  • Simplify the construction of complex expressions
  • Use a better editor

A simple way to fix this from a user point of view is to use, instead of a one-liner, a more “ProgrammableAnnotation” approach, where a specific variable, let say, output, should contain a string at the end of the execution of the python script. This string is then processed by the same mechanism as the current python annotation.

This basically fixes all three problematic exposed here.
Of course, we could ensure retro compatibility by still evaluating one-liner as a potential output string directly.

Alternatively, adding a field for the user to specify which python modules to import before evaluating would also be acceptable.

What do you think @utkarsh.ayachit @cory.quammen ?

Can you provide an example please? It’s unclear to me why not just use a Programmable Filter, in that case? Is it that Programmable Filter cannot easily produce “text”? If so, maybe that’s what we need to fix.

Can you provide an example please ?

A simple example is to show the current date using datetime module, but one could think of anything not already imported. platform, sys come to mind.

Is it that Programmable Filter cannot easily produce “text”?

Unless I’m mistaken, there is no way to create a text representation out of a programmable filter. I did not investigate how complex it would be but it would definitelly be more complex that adding a new output type as the representation needs to be a vtkTextRepresentation.

My vote would be to see how we can make this possible in Programmable Filter (or a new and improved version of the same). That should largely make Python Annotation obsolete, which sounds like a positive development to me.

That would indeed be a good idea, as long as all PythonAnnotation feature are available, especially the matplotlib rendering.

Note, the Matplotlib rendering is not unique to Python annotation, it’s supported for all text representations even when the source is simply Text or Annotate Time. So yes, it would be supported.

1 Like

After a quick look, adding a ProgrammableAnnotation filter with a Text OutputPort hints would be a simple solution.

Of course, the output would have to be a vtkTable with a string array. As long as it is documented, it should be fine imo.

I will give it a quick try.

Indeed as simple as that.

pdo =  self.GetTableOutput()
ca = vtk.vtkStringArray()
ca.InsertNextValue("$\sum_{i=0}^\infty x_i$")


Indeed its simple and helpful

Can we do the below:
Pass variables from programmable filter to annotations

Yes, connect the ProgrammableAnnotation to the ProgrammableFilter in the pipeline.

Thanks was able to manage to pass variable from a Programmable filter to Python Annotation