


Packages are directories/folders that can host multiple files (aka modules) and can be run independently of other packages, so we can host our app and its components inside of a package, with our views, and models as modules inside the package. Great news we can do it with python packages. So let's do something - what if we could have our models in a separate file and create instances from there as well, and also put our routes in a separate file, that would be great, wouldn't it. The above is what our simple, single-file app looks like, it has the basic pieces of a simple functional web app, but as you can already notice it only has two routes and is over 50 lines of code. commit () # run appĮnter fullscreen mode Exit fullscreen mode
#Flask blueprint db password#
User1 = User ( name = "Paul John", email =, password = "pjmaxson2020#" ) user2 = User ( name = "John Doe", email =, password = "jdmaxson2020#" ) db. dump ( users )) if _name_ = "_main_" : db. route ( '/getusers' ) def getusers (): users = User. route ( '/' ) def index (): return "Welcome" app. Schema ): class Meta : fields = ( 'id', 'name', 'email', 'password' ) user_schema = UserSchema () users_schema = UserSchema ( many = True ) # app. String ( 50 )) # JSON SchemaĬlass UserSchema ( ma. Integer, primary_key = True ) name = db. config = f "sqlite:/// /dev.db" # sqlalchemy instanceĭb = SQLAlchemy ( app ) ma = Marshmallow ( app ) # modelsĬlass User ( db. getcwd () app = Flask ( _name_ ) # configĪpp. Run clause (which is where we run the app)įrom flask import Flask, jsonify from flask_sqlalchemy import SQLAlchemy from flask_marshmallow import Marshmallow import os basedir = os.Database schema for JSON representation.Our single-file app (called main.py) will have the following in it We'll begin with a simple one-file flask app, and use a package to scale it a bit and see how that favors our development process. Before we jump straight into using blueprints, let's take a look at how we can get things going with python packages and see how far that takes us. It gets really messy and instinctively you might feel the need to split things into separate files. But you quickly realize how much of a problem this one-file app could be as it gets really clogged up with different pieces of code, as you could, later on, need to even write models for database, and in some instances even schema for data representation in JSON for your server. This is fine and it serves our purpose for simple small apps as we progress down the learning path. These single module apps would contain our flask app instance, routes, and even the run command - usually under a "_ main_" clause. Most beginners learn flask by building simple single module (single file) apps.
