I find the forms function on Colaboratory extremely useful when you're sharing your Colaboratory notebook for a wider audience.
Some users might not really understand Python to edit the variables for their use cases safely. Hence, the forms function provides a dialogue-box interface to enable editing of variables via an interactive format.
You can enable a form or dialogue box adjacent to your code cell that will be linked to update variables on the code cell. Here's an example:
In the screenshot above, the slide is interactive and can be dragged around to change the number from zero to a hundred.
To use it, click on +form at the right-hand side of the code cell.
Click on the pencil icon to edit the title of the form to something that will help people understand the purpose of the form. There will be a commented header on your code cell that you can just ignore.
At the right-hand side, click on +field to add a variable that is linked to the form. In the lightbox dialogue box, key in the variable name (following Python's variable naming convention) and specify the variable type and form type.
After that, a variable will appear in the code cell. Editing the form input on the right will change the variable in the code-cell. You can ignore the commented section beside your variable as that is editable via the pencil icon in the form section.
You can also click on the drop-down on the top-right side of the cell to display only the form. This way, your user can still run the code while only seeing the form-input.
Hope this post has been useful. Do use this awesome feature to make your code more shareable so you can show how awesome your work is.
Like the post? Consider donating to fund the maintenance of this website: