![]() Let's test out the routes on the server (we wrote a few after all in views.py), so open your browser and open the first route we specified in views.py which is just '/', and then the /getusers route which should return a JSON formated response lets see what we get: Now that that's outta the way let's run our server using the run file, simply do:Īnd you should see the server logs as follows: Once it's done executing, (we didn't add any logs), but you should see a new dev.db file in your project directory, as shown below: Let's test this out shall we!, let's run our models.py file to first create the database and the tables as well as some data on the tables, (we already wrote all these btw in models.py) "dunder" init.py file: contains our flask app instance and its config only, as follows:.The following lists what each file will contain in detail: In "core", we created a "dunder" (double-underscore) init.py file, which is what helps python recognize the folder as a "package", under which we add a file for our views and one for models. Let's make our package, shall we - so I created a new folder for our new package called 'core/', as well as our views and models files, as follows: 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 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |