One of the last think almost every developer wants to do is writing documentation. Meanwhile, it will require a lot of developer self-discipline to not slip some low level calls (like accessing the database) in the frontend code. We will see later in this post how we can achieve this. (If you don't know it, just search for Big ball of mud or spaghetti-code)įor this reason I would heavily recommend you to have independent routes for the frontend and for the API (a simple prefix like /api should work) but also different modules or files. And we all know what that means in the end. This, that in theory is a good thing, can lead to an entangled codebase. One of the dangers I see to a framework like flask is that the framework is so flexible that allows you to use it as a front-end server but also as a back-end providing only APIs. With this, my mind started thinking about other features that my application needed and I realized that it needed to be super modular. I needed to refactor the code and extract the logic in their independent endpoints. However, for this project I also needed to create some APIs that might or might not be deployed together with the frontend, and I found my first mistake: everything was done by the same methods that served the frontend. When you just have a couple of endpoints it can be OK to do so, and thus I started adding new methods. So my first experience was creating a single python file and adding every function there. So since I had a project in mind that needed a frontend, I did not want to go back to PHP and I've recently used Flask to do part of an AI and ML specialization capstone, I thought it could be a great idea to explore it beyond a simple proof of concept. I was a self-taught coder and I didn't even knew that in PHP you need the semicolon after each clause. While my professional experience has been in the data analysis and engineering area, my first contact with programming was creating websites with HTML, PHP and CSS. In this post, I explain you my takeaways on how to build better architected apps with it, although some are not only limited to this framework. Those have been some of the more enjoyable days I've had in a while (referring to coding). For a personal project I've recently spend 3 days learning and coding a website+API with flask. status_code = 200 assert b "Results found: 0" in response. get ( url_for ( "example.index" )) assert response. override ( github_client_mock ): response = client. Mock ( spec = Github ) github_client_mock. data def test_index_no_results ( client, app ): github_client_mock = mock. data assert b "owner2-avatar-url" in response. data assert b "owner2-login" in response. data assert b "owner1-avatar-url" in response. data assert b "owner1-login" in response. status_code = 200 assert b "Results found: 2" in response. Mock ( login = "owner2-login", html_url = "owner2-url", avatar_url = "owner2-avatar-url", ), get_commits = mock. Mock ( html_url = "repo2-url", name = "repo2-name", owner = mock. unwire () def test_index ( client, app ): github_client_mock = mock. fixture def app (): app = create_app () yield app app. """Tests module.""" from unittest import mock import pytest from github import Github from flask import url_for from.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |