# Python Annotation Improvements

**URL:** https://discourse.paraview.org/t/python-annotation-improvements/4828
**Category:** Development
**Tags:** proposal
**Created:** [July 13, 2020, 1:39pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828 "2020-07-13T13:39:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 1:39pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/1 "2020-07-13T13:39:58Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [July 13, 2020, 2:07pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/2 "2020-07-13T14:07:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 2:12pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/3 "2020-07-13T14:12:28Z")

</div>

> 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.

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [July 13, 2020, 2:28pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/4 "2020-07-13T14:28:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 3:02pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/5 "2020-07-13T15:02:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [July 13, 2020, 3:24pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/6 "2020-07-13T15:24:57Z")

</div>

> [@mwestphal](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 3:45pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/7 "2020-07-13T15:45:54Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [July 13, 2020, 3:54pm UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/8 "2020-07-13T15:54:50Z")

</div>

Indeed as simple as that.

```
pdo = self.GetTableOutput()
ca = vtk.vtkStringArray()
ca.SetName("Text")
ca.SetNumberOfComponents(1)
ca.InsertNextValue("$\sum_{i=0}^\infty x_i$")
pdo.AddColumn(ca)
```

---

<div class="post-metadata">

### Author: ![cfd\_payne](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@cfd\_payne](https://discourse.paraview.org/u/cfd_payne)
#### Post date: [November 17, 2021, 10:06am UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/9 "2021-11-17T10:06:20Z")

</div>

Mathieu,

Indeed its simple and helpful

> [@mwestphal](#):
>
> Indeed as simple as that.
> 
> ```auto
> 
> ```

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

 ![image](https://discourse.paraview.org/uploads/default/original/2X/9/9534bcaa7f85a5f2438b9e872b07b1fc40d7169e.jpeg)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [November 17, 2021, 10:07am UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/10 "2021-11-17T10:07:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cfd\_payne](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/c/e68b1a/32.png) [@cfd\_payne](https://discourse.paraview.org/u/cfd_payne)
#### Post date: [December 1, 2021, 7:11am UTC](https://discourse.paraview.org/t/python-annotation-improvements/4828/11 "2021-12-01T07:11:52Z")

</div>

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

 ![image](https://discourse.paraview.org/uploads/default/original/2X/e/e9dd2f1ed422575e596a62924478f367e1c07ac6.jpeg)
