Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form tutorial #7568

Merged
merged 3 commits into from
Jan 22, 2025
Merged

Add form tutorial #7568

merged 3 commits into from
Jan 22, 2025

Conversation

MarcSkovMadsen
Copy link
Collaborator

Other frameworks like Streamlit and Reflex have components and tutorials for creating forms.

In Gradio the input is almost always a form.

We don't have anything explaining how to create a Form with Panel. This PR addresses this issue.

```python
class FormState(param.Parameterized):
name = param.String(default="", doc="The name of the user.")
email = param.String(default="", doc="The email of the user.")
Copy link
Collaborator Author

@MarcSkovMadsen MarcSkovMadsen Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could add a regex to the email parameter. I tried but the exceptions where raised in the terminal. It was not clear to me how to easily integrate them with the components and UI.

It would also be nice to be able to add a custom validate function to any param.Parameter. This seems not supported either. Its a standard thing for all other Model frameworks I know.

Copy link
Contributor

@ahuang11 ahuang11 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think regex works as long as you don't use name as your param name because the name property overrides the param.

image
import param
class FormState(param.Parameterized):
    n = param.String(default="", doc="The name of the user.", regex=r"^[A-Za-z ]*$")


form_state = FormState(n="ABC")
form_state.name

# this fails
form_state = FormState(n="ABC123")

Copy link
Collaborator Author

@MarcSkovMadsen MarcSkovMadsen Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The problem is not the regex not working. The problem is that the regex errors are not propogated to the UI. The errors stays on the server side and are hard to catch and propagate to the user. I.e. its not helpful in a Panel context.
image

FORM_TEXT,
)

error_pane = pn.pane.Alert(
Copy link
Collaborator Author

@MarcSkovMadsen MarcSkovMadsen Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be really nice to be able to display the errors together with the widgets. For example via red borders and tooltips. The red borders can be done with a lot of work. In theory tooltips could be added. But they will be in the tooltips icon instead of over the input element.

As a workaround I used an Alert pane.

doc/tutorials/intermediate/create_form.md Show resolved Hide resolved
object=form_state.param.validation_message,
visible=form_state.param.is_not_valid,
alert_type="danger",
stylesheets=["p {margin-bottom: 0}"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if its a general problem that margin-bottom of the Alert paragraph is set to 1rem. Maybe it should be set to 0 in general?

@MarcSkovMadsen MarcSkovMadsen added this to the next milestone Dec 23, 2024
Copy link
Contributor

@Coderambling Coderambling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tutorial! Would it make sense to add 2 examples of conditional logic?

So 1 case where an input field is shown / hidden based on, for example, a yes / no input value in a previous field?

And one where the options in a field are determined by the input in a previous field?

I.e. Field 1 -> Choose continent, field 2 -> Show subselection of countries that belong to that continent.

As both are very common use cases for forms, and param seems a natural fit for the type of logic.

In a broader context, would a panel form (builder) component make sense?

Is there (a lot of) latent demand for this?

@philippjfr philippjfr modified the milestones: next, v1.6.0 Jan 20, 2025
@philippjfr
Copy link
Member

My feeling is that this is something we definitely need to work on. This tutorial sets a baseline, warts and all, and it'll be our job to improve on the status quo from here. I'll merge as is for now.

@Coderambling Yes, I think those are all good points and we should add those later.

@philippjfr philippjfr merged commit 6001acd into main Jan 22, 2025
9 checks passed
@philippjfr philippjfr deleted the enhancement/form-tutorial branch January 22, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants