Python, a language traditionally associated with server-side development, is breaking into the front-end scene with a new breed of web frameworks that allow developers to write declarative Python code for generating HTML, CSS, and JavaScript. This paradigm shift enables developers to create interactive web applications with nothing but Python, eliminating the need for JavaScript expertise.
The concept of transpiling Python to JavaScript or running Python on the front end via WebAssembly has been around, but its current implementation is clunky and primordial. However, an emerging family of Python web frameworks is changing the game by allowing developers to describe front-end code using back-end Python code. These frameworks generate front-end objects represented through the Python object model, making it possible to write Python code that programmatically generates HTML, CSS, and JavaScript.
One such framework is Anvil, which boasts the tagline "Build web apps with nothing but Python." Anvil offers a cloud service with visual build tools and various hosting options, as well as an open-source runtime that allows developers to build and run Anvil applications with hand-written code. Anvil applications consist of three components: the UI, client-side code transpiled from Python to JavaScript, and server-side Python code. The Anvil cloud editor automatically generates back- and front-end code, similar to tools like Qt Design Studio.
Anvil's cloud incarnation provides a powerful visual design tool for UIs, complete with prebuilt UI components, data sources, and a fast development cycle. Developers can write applications by hand using the Anvil runtime, with changes reflected immediately on the application server. User interface elements are essentially Python class instances, with event handlers added via class methods. It's also easy to programmatically add behaviors using well-thought-out general methods.
Another framework, Reflex (formerly Pynecone), doesn't include design tooling but shares the same underlying idea: using Python code to write the back end and programmatically generate the front end without needing to write JavaScript. Reflex compiles to a React application, with FastAPI serving the Python back end. It comes with many built-in components, including data display objects, feedback controls, and overlay objects. Developers can also wrap custom-built React components and connect to data sources using the React data layer and SQLAlchemy ORM.
Reflex has the handy ability to export an entire site's front end to a static build, making it useful as a programmatic site generator tool. This feature, combined with its ease of use and extensive library of components, makes Reflex an attractive option for developers looking to create web applications quickly and efficiently.
NiceGUI, the third framework, uses a declarative syntax to describe what UI components look like and how they behave. It includes a vast gallery of prebuilt UI components, from common widgets to sophisticated applications like audio or video, interactive data presentations, and 3D visualizations. NiceGUI handles all in-browser code, allowing developers to focus on writing Python code that describes the UI and its behavior.
NiceGUI is particularly useful for wrapping existing Python apps with a web-based UI, requiring minimal learning and effort. It uses FastAPI as its internal web framework, making hosting and deployment a breeze. Developers can also use a prebuilt Docker image or bundle their app as a standalone executable for easy redistribution.
In conclusion, Anvil, Reflex, and NiceGUI are revolutionizing the way we build web applications by enabling developers to write declarative Python code for generating front-end HTML, CSS, and JavaScript. Each framework has its unique strengths and use cases, but they all share a common goal: to make web development more efficient, accessible, and Python-centric.