Learn Flask With an App Fiddle
Learn Flask by fiddling with a complete app and database with VSCode in your browser, and discover how to create projects instantly.
Join the DZone community and get the full member experience.
Join For FreeThere is a conventional approach to learning a framework such as Flask: learn with a tutorial, then build something small, and gradually increase functionality. There are dozens to hundreds of such tutorials, and they are very helpful.
Here we offer a complementary approach, one that entirely reverses the script. Build a complete running project you can explore within a minute, then learn how to alter it, debug it - and then how to create it, literally in seconds.
App Fiddle: An In-Action Flask Tutorial
Tools like JSFiddle are extremely useful. Without installation, you can use your browser to explore existing JavaScript/HTML code, alter it, and see the results.
Here, we apply this approach to an entire app: an App (Flask) Fiddle (link at the end).
- Like a JSFiddle, it opens in your browser; no install.
- But it's a complete Flask app: a running project, with a database, accessed with SQLAlchemy
- Accessed via VSCode, running in your browser, courtesy of Codespaces
- Codespaces is a remarkable new product from GitHub. When you click the link at the end, it requisitions a server, installs your project (and all its dependencies, such as Python and Flask), and opens it in VSCode in your browser.
- You can also use this App Fiddle to explore Codespaces, how to set up a dev container, and use it on your own projects.
The link (at the end) actually opens 3 projects. The first is a minimal Flask/SQLAlchemy app. It has a README: use it to explore the code, run it, alter/debug it, etc.
Deliver While Learning
But that's not all.
While the first project shows it's pretty simple to create a single endpoint, gather some data, and return it, it's a lot more work to create an entire project (multiple endpoints, an Admin App, etc). That's a horse of an entirely different feather!
So, we've created API Logic Server. It's an open-source Python app, already loaded into our Codespace project.
It creates an entire Flask project with a single command, like this:
ApiLogicServer create --project_name=ApiLogicProject --db_url=nw-
This reads your database schema (here, a version of Northwind) and creates a complete, executable project, instantly:
API: An endpoint for each table, with filtering, sorting, pagination, and related data access. Swagger is automatic.
Admin UI: Multi-page/Multi-table apps, with page navigations, automatic joins, and declarative hide/show. It executes a YAML file, so basic customizations do not require HTML or JavaScript background.
- Custom UIs can be built using your tool of choice (React, Angular, etc), using the API.
Fully Customizable: Standard Python, Flask, SQLAlchemy
Creating the executable project requires no background in Flask, SQLAlchemy, or even Python. In fact, you can use the created project to learn these technologies, by "fiddling" with a running system that's already delivering value (e.g, enabling custom UI dev, integration, etc).
That's because the created project is a standard Flask/SQLAlchemy project. Customize and extend it with all the fundamentals you learned in conventional tutorials, and in the App Fiddle, with your favorite IDE.
Unique Spreadsheet-Like Business Rules
As an experienced app developer, I think of projects as about half the back end and half the front end. Your mileage may vary, but the backend is certainly a lot of work.
- Multi-table derivations and constraints applied on update:
- E.g., the customer's balance - the sum of the unshipped order amounts - cannot exceed the credit limit
- Authorization and authentication
- E.g., users must enter a valid id and password to gain access
- Their roles determine what database rows they see (e.g., a multi-tenant application).
API Logic Server enables you to declare spreadsheet-like rules to implement these. Rules are a very significant technology, but perhaps the most striking characteristic is that they are 40X more concise than code. These 5 rules represent the same logic as 200 lines of Python:
Rule.constraint(validate=models.Customer, # logic design translates directly into rules
as_condition=lambda row: row.Balance <= row.CreditLimit,
error_msg="balance ({row.Balance}) exceeds credit ({row.CreditLimit})")
Rule.sum(derive=models.Customer.Balance, # adjust iff AmountTotal or ShippedDate or CustomerID changes
as_sum_of=models.Order.AmountTotal,
where=lambda row: row.ShippedDate is None) # adjusts - *not* a sql select sum...
Rule.sum(derive=models.Order.AmountTotal, # adjust iff Amount or OrderID changes
as_sum_of=models.OrderDetail.Amount)
Rule.formula(derive=models.OrderDetail.Amount, # compute price * qty
as_expression=lambda row: row.UnitPrice * row.Quantity)
Rule.copy(derive=models.OrderDetail.UnitPrice, # get Product Price (e,g., on insert, or ProductId change)
from_parent=models.Product.UnitPrice)
The third project in the fiddle illustrates both the rules and some "standard" Flask/SQLAlchemy customizations. A tutorial is included to help you explore these, run them, see how to debug them, etc.
You can access the app fiddle by following the links at the top of this blog.
Opinions expressed by DZone contributors are their own.
Comments