Elegant Web Development With Emmett In Python | by Emmett Boudreau | Towards Data Science

Python web-development is a portion of Python programming that has been dominated by popular options such as Flask and Django for years. Those solutions are certainly great, and I have used them myself a lot. That being said, I think there are shortcomings to any software, as certain directions have to be taken, and some software is better at some things than other things. While actually looking to find an article I had written on machine-learning validation, I stumbled across the Emmett project.
Who would have thought that I would have a web-framework in Python named after me? Just a joke of course, the framework is actually named after Doctor Emmett Brown from Back To The Future. The logo for the framework sports his face with his iconic goggles on. I actually happen to be quite the fan of that movie, not the second one though (what even was that?) and given the name, the package peaked my curiosity, and I decided to try it out. If you would like to look more into the package, you may check it out on Github here:
github.com
I should start by saying that I typically do not expect much from these kinds of things, as there are so many great examples of web-frameworks available with better documentation and a lot more users. However, reading over the list of features, and taking a look at the methodology, I was certainly interested in this weird package. I cannot tell a lie though, it was weird importcuriousitying things from myself whenever I gave it a try. But I am very excited to show off this framework and what it is capable of! I also have two articles I have written in the past that discuss other solutions besides Emmett, Django, and Flask you may read here:
towardsdatascience.com
Or here:
towardsdatascience.com
The question that arises with any example of a web-framework coming to fruition, why choose Emmett over more popular solutions such as Flask or Django. When we compare Emmett between those two solutions, we should note that Emmett is in a lot of ways a combination of those two. Django is more commonly used for fullstack applications, and Flask is more commonly used for more data-driving, and simple applications. Both have a different methodology and syntax. Emmett is in a lot of ways, a great middle-ground between them.
The syntax is pretty close to Flask, and so is the methodology. Personally, I much prefer this. I never really liked the overwhelming templating nature of Django. While there is certainly nothing wrong with it, it generates a lot of directories and files to dive into right off the bat, rather than having the user create their own. Emmett acts in between this, but instead of relying on directories and files, the module calls are used instead, which I think is a lot more elegant. If you would like to read more about the differences between Flask and Django, I have another article that goes into a lot of detail on that:
towardsdatascience.com
First and foremost, Emmett has a methodology that is meant to be approachable and easy to use. With syntax similar to Flask’s, most users will find themselves at home when working with the framework. However, the framework is not as lightweight as Flask, meaning that there are a lot more great features that are built in than one might expect with Flask. It writes like Flask, but it works more like Django.
The module comes with database classes that make it easier to query things, tasks, and even authentication. All of these in my experience with the module have also been a lot easier to use than Django’s implementation. The project is also claimed to be production ready, which is certainly great for those who want to use this module in their tech stack.
Another cool thing about Emmett is that it is asynchronous. Websockets are awesome! Emmett is designed atop of asyncio, a venerable library for this sort of thing. This methodology makes Emmett non-blocking, and a lot more usable for many design patterns. Another cool thing is that the package comes with its own query interface.
The last cool thing about Emmett is the templating. In most web-frameworks, templating is often a complete mess. Sometimes they have their own templating framework that requires learning a new language, sometimes you might need to write some HTML and/or Javascript along with your Python in order to get the job done. Emmett has a quite robust templating system without all of the headaches of these traditional systems. The templates are coded in pure Python!
Using Emmett is incredibly simple and very approachable in my experience with the package. The documentation is also quite exceptional. You can install the package with
pip3 install emmett
Of course, I am no web-developer. What I look for in a web-framework might be different to those that traditionally use Python for full-stack. What I lack also happens to be what Emmett has, so that is definitely pretty cool! Emmett actually includes a great and easy to use querying framework that we can use, and I think it is actually very impressive. Check out the following example from the documentation where we create a schema with users, posts, and comments in only a few lines of code!
from emmett import session, nowfrom emmett.orm import Model, Field, belongs_to, has_manyfrom emmett.tools.auth import AuthUserclass User(AuthUser):    # will create "users" table and groups/permissions ones    has_many('posts', 'comments')class Post(Model):    belongs_to('user')    has_many('comments')title = Field()    text = Field.text()    date = Field.datetime()default_values = {        'user': lambda: session.auth.user.id,        'date': lambda: now    }    validation = {        'title': {'presence': True},        'text': {'presence': True}    }    fields_rw = {        'user': False,        'date': False    }class Comment(Model):    belongs_to('user', 'post')text = Field.text()    date = Field.datetime()default_values = {        'user': lambda: session.auth.user.id,        'date': lambda: now    }    validation = {        'text': {'presence': True}    }    fields_rw = {        'user': False,        'post': False,        'date': False    }
We can route the app using the @app syntax. This is very unique and cool in my opinion, we use the @app.command() method and then we can call globally defined functions for our app to work with from there! Quite a popular Data Science and Data Engineering task is creating a simple endpoint, here is the code to do that with Emmett:
from emmett import App  app = App(__name__)  @app.route("/") async def hello():     return "Here is a simple endpoint!"
As you can likely tell, this endpoint is very similar to those made in Flask! In fact the code is nearly identical. However, it is easy to see that this framework is no Flask and brings along a whole load of features. In my opinion, it is really cool, high-level, and so declarative at times it seems to be just too easy to use. I do not often find myself falling in love with random packages I find like this one, but it is certainly worth checking out! I know I will be using it in the future! If you are more interested in Flask, I do have another really old article (from 2019!) that you can read to learn more about deploying endpoints in it!
towardsdatascience.com
Although it is a bit unsettling to be working with a package that has my name at times, this package is certainly worth dealing with that uncanny valley feeling. I really enjoyed using this package, and realizing how cool it was over and over again was shocking, to say the least. Of course, when I use a package like this, I always compare it to what I already use. Probably the choice I use the most in that regard is Flask, I really love how Flask can be both small and big. This web-framework, however, leaves me completely speechless on taking that same methodology and drilling it to the core. If I had to describe it in a sentence Python programmers would understand well, I would say “ It is like Flask, only with tons of amazing and useful features.” I certainly recommend this module, and not just because we share the same name. Thank you for reading, and I hope you consider trying this package out — It is awesome!