 greetings and welcome everybody this is a talk about Django about building a Django clone from scratch with a flask and well that is our Python user group's logo I am from Russia which is a small island in the Indian Ocean and our user group is called PyMug the Python user group of Mauritius you can view more about this on this provided link and a bit about me I am Abdulrahman Jahangir from well from Mauritius I'm a Python programmer for myself that you can find my Twitter you can find me on GitHub and you can find more about me on this URL also a flask on organizing member yes let us begin our talk and we put in the disclaimer that anybody following this talk should know both flask and Django should have used the flask and Django to build apps should have mainly used flask and also Django to build at least some apps so that you understand what the talk is about what is going on and how this talk might be beneficial for you to what degree it is the talk worth it so you'll be able to judge if ever you used both flask and Django so the idea about closing cloning Django it is about seeing what the aspects of Django makes the development experience interesting Django is widely popular because maybe in my opinion it's easy to to to set up it's easy to use it provides lots of functionalities out of the box and at the beginning it was used in an environment and the high pressure people liked it and we can say that most of the freelancing jobs today require Django if you go to even a freelancer.com you go to the all sheaves you will find that the most of the freelancing jobs for Python for web is in Django so we might ask ourselves what the aspects of Django makes the development experience interesting what aspects of Django makes it worth it to stick with Django to work with the Django well in my opinion these are some aspects that are really cool these are some aspects that in this talk will show how we can replicate those features using flask and we'll try and this one the first one is about modules well Django has modules with modules granular migration that is not only migration can control what you want to migrate single point of operation that is you don't have to run several files you it provides a central point of operation the queries are very cool the or the syntax of the OM is very cool you also have a wide number of packages online available and you have support tools meaning some rest API's etc are presented as a product oriented packages close base views generic views context bossing and you have the admin panel the admin panel which is wonderful experience for anyone is starting out that you just get to interact with your project products magically like out of the box you can add product can delete product you can manage products you can do what ever you want with your models it and you just get it free out of the box and that is something really cool well what we mean about modules is that Django allows you to do to have your forms to have related form codes to have related logic to have related views to have related models to have related URLs all in one place all in one folder with when normally when you make when you when you run the command Python Python is taught at Django you have Django Django automatically or I might say when you run the startup command Django automatically creates for you your some some files the folder and even some codes inside so that is what we get out of the out of the box with the Django and also the modules are just there and then you have to specify that you are installing the module that is you are using the module so what it is about grouping your related concerns together you get it with the Django and with modules your code is more organized is more like loosely coupled you might even share it with a friend and to hopefully he'll have it on his and he can interact with it he can use it and it is easier to get the big picture of what is going on with your project with modules like you know this this section this section this section you know exactly where the code is found where the logic is found and I heard of a case of someone who was using Django and he wrote like about over one GB of code before someone else took over and it was undocumented why in my opinion it's because it was clear to him in his head how things work how things are organized he didn't get lost even in a one GB could be so it modules make your code really organized and yes a gradual granular migration where the Django provides you with the migration mechanism out of the box first it gives you the ability to migrate out of the box and you can control what you want to migrate if you don't want to migrate everything that is your choice but Django provides you with the ability to do whatever you want and what that means for example Laravel Laravel is a Django inspired framework and Laravel has what we can say yes Laravel was inspired by Django and Laravel has gradual migrations if you organize your code into modules into a if you have a modular architecture in Laravel you can choose what to migrate and this results in less pollution like you don't migrate every change at once you choose what to reflect in the database and that Django provides out of the box and yes a single pointer of operation with a manage.py you can have a single file just input the file the command that comes after like you don't have to run a run.py you don't have to run a migrate.py you don't have to run and apply settings.py and yes these are all about the query Django provides cool queries out of the box and beginners I mean people just starting out people just starting it can be daunting but on the other hand it is really nice that just as you are beginning you just you just get to use the OIM just get to use the query which is really powerful yes you have powerful queries in simple lines of code and beginners get exposed to serious concepts from the beginning like you have you are using an OIM as a beginner which is very cool like which maybe you would have to wait for quite some times if you were through another route and the way if you if you use Django and the way Django is organized and the way Django does things I can say that it has a lasting impact on the way you write code on the way you do things because you've been exposed to a nice system to a nice way of doing things and yes Django has a wide a really wide range of choice for packages for example you have Django packages.org which is a an unofficial if I remember well. Repository for packages currently it has more than 3000 apps and that is really cool which is it's really great that people just share their a part of their of their a part of an application which you can just pull in and start using it which is it is something really amazing like it's great it's I can't describe it yes and the modules are plug-and-play you put it and you specify but basically it just works and that is about the packages support tools as packages for example Django mini packages are for debugging or for profiling or for testing your endpoints those packages are really great like they are not only for product oriented but they also like for debugging to to help you with your coding and these are some packages that you find open yes and Django has cloud space views it has some crud it gives you a nice advantage when you are using crud operations and which is really nice because you are using only Django you are you must adapt yourself to Django's way of doing things and it simplifies for you many things those are some some advantages of Django and also Django what makes it interesting to work with Django is the way Django parses template variables as you know you just pass a dictionary and normally in Floskey the normal way is to have optional parameters so passing template variables as as a dictionary is a really nice idea and you have yes the admin panel out of the box with which allows you to to work with your models like quite easily it also you can also out of the box interact with your users you can out of the box interact with the with permission groups the admin panel is really powerful and running in production.com it has a podcast with Tan better if i'm not mistaken and there we i was surprised to discover that to to know that real python actually uses the built-in Django admin for its editing for everybody to collaborate and it uses like it's possible it's a possibility in this time and era to have solidives and that's him working on the side which is really great the admin panel and this is and when i pinged my local friends so they said that this was one point that Mr Dominic he said that one point which makes it really useful to for him really interesting for him to work with the Django is the admin panel so i guess that the admin panel really makes a difference like sometimes you just want to to know the result to view what is happening now now it's a bit about those are like some features that we'll try to replicate in our app but this section is about what i don't in a clone that i'm building like if ever what i would want to to build something like Django from from scratch what i don't want to include so these are some points one it's about the oom well one point why why i i like flask i i don't like Django so much it is about the Django orm which which well it is not a complete orm but which is the root taken by many orms but compared to sql alchemy so the Django orm is not since it's simply not up to the level you must use for complex queries it is better to go with raw sql which kind of defeats the purpose of having and using an orm and many Django divs at the one at one point they they choose a web framework just because the orm is not up to the level just because they can't use some simple join queries which is like somewhat simple in sql and if you want if ever you want to have an overview on how the Django orm and sql alchemy differ and why sql alchemy is better than the Django orm the default Django orm then you can refer to to this article by mr. omin Rulaker about sql alchemy and you and what i mean about Django experience devs experience the Django developers switching over like the orm like growing out of the orm like quitting Django just because of the orm and i think if ever the Django developers the at one point or the other they decide to cancel their orm and use sql alchemy instead so i think it will be a very good move which will improve the adoption rate of Django and it will really properly really nice and though it is a undoubtedly very popular now but this is what the author of bwee has to say that he used the Django before but uh being a veteran sql uh user he did it uh he didn't feel feel comfortable comfortable with the default Django orm that is him and uh you know when when a an orm is great when for a question for example has anyone regretted using sql alchemy and this user who has been writing sql for quite some time he has to say that he searched for sql alchemy and he just doesn't miss uh row sql since the last two years whether your library is great even for when someone requests for these advantages you get you get advantages you get someone who has been using row sql turning to sql alchemy and of course sql alchemy gives you control over inflexibility over your your objects and uh yes when you use Django so you have uh some default apps uh coming along tag some default uh tables uh coming along tagging along like you haven't yet it started too good but your database is already filled but i know it's uh the case with event WordPress etc but uh if you want to to to to have a clean look at your database so you off with uh with you start with uh some some some some tables and uh one confusing point uh when you you teach Django it is about the url so you have several url.py and you ask uh students you ask people you ask attendees to modify such and such thing in the url.py but they end up modifying another url.py they end up like uh it's it's difficult to differentiate between those two like oh there are actually two types of urls.py one is for the modules and one is uh actually for the whole app to to map the the views but uh you it's not a problem for experienced users obviously and also why i what i don't like with the Django it is about having all apps on the same level you have your primary app on on on on one level and all all the apps you have it on the same level and you have to remind yourself like what is the name of my application and oh those are not my primary app so you have to to to kind of remind yourself you have to kind of uh because as you put new applications your your primary app moves up and down but that should not happen at least according to me yes now let us talk about our concretely speaking about our prototype and how we we want to implement it the the features we we discussed can we implement it how to implement it but it is to be to be noted that we chose a we'd not we chose it as near as possible to to Django how a Django application is but obviously we preferred the to keep at least one aspect of the flask spirit flask default working which is having the decorator above the function but we also discussed implementing clause based views in flask yes i said the Django clone from scratch using flask and not Django clone from scratch using python this is the architecture i can say of the application well you have app dot py manager py settings dot py dmsji dot py but you also have you and you have a foot a folder called modules in and in the folder called modules every module has to be in a folder and inside the module folder you have templates folder you have models forms and view files and then inside the templates folder you have another folder called blog same as same as the module name and that is why yes you might wonder in the last slide why in the templates folder there is another folder called blog it is because when flask will look for index dot html and if it finds several index dot html so it won't return what you expect it to return but if you put like some kind of name spacing in the templates if you put blog if you put like a folder name blog so you can refer to this particular html as blog slash index dot html which will be more clear in the coming slides yes this is for render template so you don't just want to render index dot html but you render blog slash index dot html and the app knows exactly which index dot html you are being pointing and the project which prompted me to write to to give this talk it is a project called chirpure which stands for shop plus python plus open and it is an ongoing ongoing experiment yes it is an experiment it is an ongoing experiment in replicating the features of jungle but more specifically about enhancing the flask development experience further modules came along this came along that came along and I say that well I'm having the features of Django I was looking for it I'm having it but the spirit behind is improving development experience using flask and you might be wondering well it's me who is saying that this app that this project I have is in Django cloud and like we clone the features of Django in it but is it really is it really convincing is it a convincing Django clone well I was talking to someone on our discord when he mentioned the project how we found how we were going over how a bit about how he found the project and then I said to him that well in this project we copied lots of Django features and he was like oh really it's not Django and I said no that's not Django and then he said well he was amazed that it's flask and then I explained to him different features that we copied and yes and so here's how we did it so here is how to implement Django modules in flask well this is the architecture that you have just a few moments for you to to analyze it to see that there is a folder called modules and inside it a folder called blog and inside that folder called blog there are your templates models forms and view now this is the layout how concretely do you achieve modules this is what we'll see so let us see about referencing let's see in in how do we reference a model from blog in modules to view.py let's say in modules too we have a view.py so how do we refer to a model which is found in blog so that's how we do it we say from modules.blog.models import let us say model called post so we import post and then let us say we are in blog in view we want to call a module find found in models like we are in the same folder how do we call it well we can call it generally from modules that blog.models import post or we can do as Django does from that models import post and also of course in view.py if you want to call something in forms.py you just say from that forms import your form now how do we to register blueprints how do we achieve the plug and play behavior well we must run now yes you do it like this you you have your let us say blueprint called people you say people blueprint you the name of the blueprint is people you template folder you put it as templates because remember we put out we name our templates folder template and the url prefix we put it as slash people so you will see people repeating at three places one is in people blueprint so that that's one place the first place where there is people the second is the name of the blueprint and the third is the url prefix and of course this is for the view but the modules folder is also named people and then not for every app but we have this snippet in our app.py which goes over every folder found in modules and then imports modules dot the modules name dot view it goes into the modules view.py and then it imports the blueprint which is of course named same as which is prefixed same as the folder's name so with this little trick we ought to register blueprints no need to to to import and to register that just you just lose time it looks for just but just you have to to to make this this convention you have to be consistent with this convention and using this snippet you ought to register your blueprints and then you just have to restart your server you get the plug and play behavior you get really plug and play behavior of course you have to migrate you have to migrate don't forget but what's what what is more important is that you get the plug and play behavior and now how do we implement manage dot py well i'm skipping imports but normally i'm using flask script you have with just these three lines you get your whole manage your whole migration management commands you need the database when you migrate and upgrade those two together form the migration command and of course you have the run server you define it as search you have the run debug and then here you have some custom commands you put you say that if by the measure py if the next argument is run server so you just apply the run server you just call the run server function run debug and then it start up what does it start up does is that instead of you remember what to keep consistent in creating modules it start up is just a helper function it just creates the files for you the folder for you and the file content for you suggests it's instead of you creating manually which effectively by the way replicates the Django's behavior python manage dot py is taught up and the name of the app and here it is the the the running the commands yes i was saying with them with our manage dot py with some simple line of code we are we already get run server we already create have the command to create our apps and it's python manage dot py is taught up robot it's a typo that i made so be careful about this yes and implementing context parsing normally in flask you render the template and then you say for template variable you say pencils is equal to five pens is equal to four like like that but you know it it is not very need to to pass parameter parameters to pass pass pass it's not very neat what you can do is you put it in a dictionary okay and you just unpack it in your render template another thing which you can do is to have for example if you have some template variables that needs to be shared among every template for example some some some values in the footer some values here some values there so you can have a global default values and then in inside each and every view function what you do you copy the the the default dictionary and then you update it and then you unpack it so that you get a similar behavior to Django's context dictionaries i know in Django some people they like to pass in locals like you they don't want to define any any you know variables but i think doing this in this way like you specify each variable so i think it's more explicit now what can we what can we customize further one it is about the admin panel i know that flask has the flask admin but and what is the beauty of flask admin is that it doesn't create your tables etc but it it it is a manager an effective manager with which its aim is to replicate the the Django the Django admin panel i feel that it it it aims to replicate it but of course it's not exactly as the admin panel so what is the solution is you go to to the empty tables that Django due to the tables that Django creates by default you see what feels there are and you will get an idea about how exactly Django managers permission how Django managers permission groups etc you will like how does it manage everything you will see the you can see if it's a square light you you go to a square light browser and you see the tables or whatever database you are using see the default tables for Django and try to implement one for you and of course share it with us afterwards yes if you are really adamant on having your real stutter py you you say well this talk is bluffing it's it misses the uos.py part and it's not complete well flask specifically implemented it for users who want the Django behavior to have uos.py so to flask provides a class based views so here it is a simple snippet taken from the flask that direct from the flask talks which is how you can do it and yes one thing i think it's worth exploring it is about metadata well what do i mean by metadata it is for example you have packages okay you have packages and you want to include some more info about maybe what other package you need to install so that this package works maybe you can have another another attribute another json attribute well you have the name you have the type show normally in shop you you can show or not show modules normally you have a control panel and we have different icons when you click it will it will it will link you to the index of the app and show we choose to whether to display the app or not it's a bit we took that concept we we were inspired by android where an application can ask not to show its icon on your phone which viruses abuse so that they they pass like incognito you you don't know if they if they exist on your phone because they ask not to not to show the the icon but here we put it because maybe some functional packages which doesn't need to show up in to show up so that's a bit why you have show and then the font awesome icon which plus i build also users appear the same idea and then you specify the the url prefix here so you get a bit more control but it also provides some more some more advantages some more options of what you can do with it in in the future and then what once you have the your info the json file in in your normal that's a quick fix that's a quick hack that's a proof of concept it's it's it needs to be improved certainly but that's how you can do it like you you have the directory part of the file and then you open the directory first part you append to it your info that json you can use us that part to join if you if you wish for the file you get the info and then you have the url prefix here you get it from your info file even the name you could have got from the info file but you would have to to be consistent with with it and maybe we can tweak it further etc but that's a that's a proof of concept that you can use you can have some metadata in your in your in your packages and yes for default users like how do you add new users that also of course the manage.py is very easy to to add your own custom function but you can also make it so that whenever you create a new project there are some initializations that get done you can have for example an initialize command that allows you to have some basis some some basic things set up such as adding a new user and in a function you add the add user to it and then you have a json where you have several you know users with the name and and email addresses etc which you which you get added by default in your database so that you can use them out of the box to test or if you implemented the panel to be able to use it spontaneously and to conclude we can see that this proof of concept that works yes that works we have what do we have what what Django features do we have one these modules with forms models views and we showed how you can implement your own us that be why if you ever want and of course your template how to make modules plug and play just like you would do with with Django how to make modules support metadata how to get the run server command how to apply migrations how to have the startup command how to have how to pass context and also exploring about the generic views how Flask provides out-of-the-box support for generic views if ever you you want and okay i i think i i i just repeated it over there but the the point below it's about having default users default settings that you can use out of the box how to customize it yes and if you ask me how to teach uh web programming how to how to to teach like back end programming i would start with it with it not deep but a thorough introduction a thorough explanation of api of requests of servers and then to teach people how to be behind the scenes them implementing that same thing they were interacting with using bare requests and from there when they get into into back end to to start the small and give them a a broad overview of whatever thing you can you can do and to let them you know come up with their own project to to make them you know really solid developers to provide them with teaching with a testing education and because you know if you just put every concept in a child at once like you know jungle does maybe it it won't be easy it won't it is not fun and but if you if you do it like incrementally you can have a better chance and it you can start i can say at a lower age and of course those who want it just just at once can can always go for it and that is one against the the link to to the project and some closing words about all flaws extensions reliable like the flaws ecosystem is it reliable well mr joker born spoke at floss gone 2020 and they spoke about his lessons building microservices with the floss and he said that the he has 65 000 requests per minute yes per minute and that's three billion rate limited requests per year and he said if you listen to dojo that we've had absolutely new issues and that is all i had to present thank you