fractal-analytics-platform / fractal-tasks-core

Main tasks for the Fractal analytics platform
https://fractal-analytics-platform.github.io/fractal-tasks-core/
BSD 3-Clause "New" or "Revised" License
11 stars 5 forks source link

Allow conditional fields & either/or logic #756

Open jluethi opened 3 weeks ago

jluethi commented 3 weeks ago

Brainstorming issue.

Can we improve what kind of json manifests are built to handle the following 2 cases better? Both examples of the Cellpose CellposeChannel1InputModel:

1. either/or logic

What if we have 2 values and a user can only select 1? See "user should either specify Wavelength ID or Label, but if they specify both, the Pydantic model validation fails

=> How could we display this better? One option that won't look so good: Have a dropdown for "how to select channel" with wavelength ID & label. And a single "value" field.

What would be very nice here: If the user enters text in both fields, json validation fails. I'm not sure if that's feasible? And how we could set up a Pydantic model that can reliably be parsed into that?

Same would apply to only setting lower & upper percentile, or lower & upper bound, but not both. We validate on the Python side, but can't easily expose this in the interface.

2. conditional fields

If "default" is selected for normalize type, a user shouldn't be able to enter percentiles or bounds. If they select custom, those fields should "show up". Again, not sure if we can specify this in a json model and how we'd get from Pydantic reliably to that json.

Screenshot 2024-06-07 at 16 16 25

One thing we discussed and discarded: The server does not run the task to try whether inputs would validate. Any improvement here cannot remove this separation. So it would need to work in json schemas that we can reliably generate from Python code, potentially with advanced features for how the developer sets them (see discussion here: https://github.com/fractal-analytics-platform/fractal-tasks-core/issues/755)

tcompa commented 3 weeks ago

What would be very nice here: If the user enters text in both fields, json validation fails. I'm not sure if that's feasible? And how we could set up a Pydantic model that can reliably be parsed into that?

I would split this discussion into three parts:

  1. Which JSON Schema best encodes the expected values? The obvious keywords coming to mind are e.g. oneOf or dependentRequired.
  2. How would we render this schema in fractal-web (let's also have a look at the other usual existing form-generators)? For both this and the previous point, let's work with @zonia3000 to define relevant examples and to explore the UI side of it.
  3. How would we generate such schema? My first guess is that Pydantic may not be flexible enough, but this should be explored further. In that case, some options would be

Points 1 and 2 are the ones which I think can already start and be discussed in this issue (or in a mirror issue in fractal-web). We should collect a few examples where either/or conditions are only enforced within Python, and look for the appropriate form of the more strict JSON Schema - so that we can then start playing with the form generator.

tcompa commented 3 weeks ago

3. My first guess is that Pydantic may not be flexible enough

Just for the record, a typical solution is to use two models for the two different scenarios (as in https://stackoverflow.com/questions/78243337/pydantic-model-with-fields-that-have-dependent-allowable-values).