Python Form Generator (JSON to Streamlit Form!)🔗
Python Form Generator🔗
From raw JSON to Python Pydantic Model to Streamlit Input Form
Bootstrapping more Streamlit apps from within a Streamlit app
Generate well-typed and validated Python forms from basic JSON example.
DEPENDENCIES:
- Datamodel Code Generator for mapping to Pydantic model
- JSON to Pydantic for proof this can work
- Streamlit Pydantic for bootstrapped app code
Full Walkthrough🔗
Skeleton🔗
GOAL: Initialize git repo with necessary file scaffold:
app.py
: The main streamlit entry apprequirements.txt
: Pin versions of Python packages so deployment and development is repeatableREADME.md
: This documentation.gitignore
: Prevent secrets or large files or unecessary things from going into github- Recommended github python gitignore template works fine for this, slightly overkill
LICENSE
: Apache 2.0 same as Streamlit Terms
Bash | |
---|---|
Install🔗
We'll plan on 4 main packages for this:
streamlit
pydantic
streamlit-pydantic
datamodel-code-generator
Bash | |
---|---|
Note: This lets streamlit-pydantic
to choose which versions of streamlit
and pydantic
work for the latest release.
Saving the output to requirements.txt
in the format:
Text Only | |
---|---|
Saving the output to requirements.txt allows new users / deploys to install pinned verions with something like the following:
pip install -r requirements.txt
Hello World🔗
Adding the following to app.py
:
Python | |
---|---|
Then run with streamlit
to check if basic input / processing frontend works.
Model Generation🔗
We'll draw from 3 sources for the meat and potatoes of this app:
Imports🔗
Expand import section of app.py
to accomodate Streamlit Pydantic, Datamodel code generator, and GenSON
(Datamodel code generator is intended as CLI tool, so we have to dig a bit into it)
Python | |
---|---|
Upgraded Input🔗
We'll use Streamlit Pydantic to get out of the box validation of the JSON that the user provides. Plus we get extensibility via the FormModel if we want to add more configuration options!
Instead of a plain text_input
from streamlit, we utilize sp.pydantic_form
and provide our model (which only has 1 input field for now!)
After getting some input JSON from the User it will pass off to converting to a model.
Conversion🔗
Following in the footsteps of JSON to Pydantic and Datamodel Code Generator, we use GenSON to build a JSONSchema representation from raw JSON data, then dump that into the Datamodel Code Generator parser.
We'll handle the same error case Datamodel code generator does, otherwise assume the happy path and display the results!
LGTM🔗
Trying out a simple entry (even simpler than the littlest fullstack app) such as the following:
Text Only | |
---|---|
Produces expected result
Python | |
---|---|
Time to ship it and generate a Form!
Bootstrapping🔗
Alright, we've got a nice Pydantic model, time to generate a Streamlit Pydantic scaffold from it and provide a download link.
Update Show Model🔗
We'll allow the user to download just the model into a .py
file and hide the generated model unless they want to see it.
Inject Generated Code🔗
We only need 2 features to really bootstrap these forms:
- Added imports
- Retrieve form data in Streamlit app
I'll add a main()
method to keep scope contained, but the Streamlit execution model embraces all Python scripting
LGTM🔗
Testing out the download and run on our simple model yields great results!
Wrap Up🔗
That's it for the basic idea!
Next steps would be allowing configuration options such as "all Optional" and snake casing akin to JSON to Pydantic. Also more input forms, as Datamodel code generator handles OpenAPI, CSV data, and more.
Created: June 7, 2023