# Proposition of Python View parameters

**URL:** https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669
**Category:** Development
**Created:** [January 11, 2024, 10:17am UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669 "2024-01-11T10:17:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [January 11, 2024, 10:17am UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/1 "2024-01-11T10:17:59Z")

</div>

Hi community,

The Python View is a 2D Render View that is configured from a python script, given as property.  
One use case is to use external tools like Matplotlib to do the rendering, like here:

 ![](https://discourse.paraview.org/uploads/default/original/2X/6/6c676e6c5bb36bb71846285190a828df6d6badb5.jpeg)

## Limitations

### User script

ParaView expects 2 methods to be defined in the script in order to works

```python
def setup_data(view):
  pass

def render(view, width, height):
  return someVTKImageData

```

This is not easy to discover, and easy to break.

### Parameters

There is no way to manipulate parameters from a GUI. All of them should be defined and modified directly in the python script. This is not a user friendly way to control a view in the application.

## Possible improvements

This View make me think about `Programmable Filter` and I wonder if we may modify the Python View to be more consistent with it.

I mean:

1. Add an API on `vtkPythonView` to add a list of custom parameters (`AddParameter(string, string)`) and the relevant XML property
2. Create a dedicated property for each method content
3. Move `setup_data` to the `Python Representation` part

Those points can be done incrementally.  
With the first, it makes it possible to create a XML plugin to create a python view with a hardcoded script while having accessible properties.

Having one property per method seems the correct way to do to me. Expecting only the function content (without `def` line) brings consistency with `ProgrammableXXX` filters, and make it easier for `vtkPythonView` to hook it up. An alternative is to let the `def function():` line be part of the property but put it as the default value so it is more discoverable.

The last point is for consistency with ParaView paradigm. This implies the previous point.

Any thought?

PythonView: [https://docs.paraview.org/en/latest/UsersGuide/displayingData.html#python-view](https://docs.paraview.org/en/latest/UsersGuide/displayingData.html#python-view)  
Workaround to control the python with a GUI: [Customize Python View with Python plugins](https://discourse.paraview.org/t/customize-python-view-with-python-plugins/11885)

similar issue: [https://gitlab.kitware.com/paraview/paraview/-/issues/20094](https://gitlab.kitware.com/paraview/paraview/-/issues/20094)

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [January 11, 2024, 10:20am UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/2 "2024-01-11T10:20:37Z")

</div>

@cory.quammen @Charles_Gueunet FYI

---

<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: [January 11, 2024, 12:28pm UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/3 "2024-01-11T12:28:55Z")

</div>

Looks nice to me. What about PythonPlugins ? Would that make the python view compatible with python plugins ?

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [January 11, 2024, 12:45pm UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/4 "2024-01-11T12:45:05Z")

</div>

> [@mwestphal](#):
>
> Would that make the python view compatible with python plugins

Nope. Python plugins are still another thing, I didn’t investigate for now.

Currently, the python plugins are `vtkAlgorithm` subclasses with inline XML. We should extend support for `vtkView` or such.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [January 11, 2024, 2:17pm UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/5 "2024-01-11T14:17:37Z")

</div>

The Python View definitely has room for improvement, and these proposals make sense. The only thing I’m not sure about is moving the `setup_data` to the representation properties. This will move where you set the script in the Properties panel, which may make it harder to find in the UI. You could probably list it last in the representation so that it appears close to the View section in the Properties panel.

---

<div class="post-metadata">

### Author: ![ltetrel](https://discourse.paraview.org/user_avatar/discourse.paraview.org/ltetrel/32/15636_2.png) [@ltetrel](https://discourse.paraview.org/u/ltetrel)
#### Post date: [January 11, 2024, 2:57pm UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/6 "2024-01-11T14:57:52Z")

</div>

As a long-time user of python matplotlib, I find this quite nice!

Is the goal to expose:

1. only the matplotlib render window (so all the configuration stays inside the python script)
2. the matplotlib parameters so no python script is used

If 1. developping/debugging the script (inside the property panel) will be hard and a solution as an external python plugin seems better  
If 2. this seems like a trendemous amount of work, and would be redundant with the existing “raw” plots from paraview ([4. Displaying data — ParaView Documentation 5.11.0 documentation](https://docs.paraview.org/en/latest/UsersGuide/displayingData.html#line-chart-view))

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [January 11, 2024, 3:08pm UTC](https://discourse.paraview.org/t/proposition-of-python-view-parameters/13669/7 "2024-01-11T15:08:19Z")

</div>

@ltetrel not sure what you mean.

The `PythonView` script should return a `vtkImageData` that is displayed directly in the view (and its not gonna change). We provide some python utility functions for data conversion, so it easier to use other tools like matplotlib inside the script to generate the final image.

What I suggest is to let the user initialize some python variable from the GUI. It is up to the dev to choose which variable to expose and to synchronize the name in the GUI and the name in the python script.
