Backend With Python, How?
Janne Kemppainen |You want to be a Python backend developer but have no idea what you should learn? Choosing what to focus on can be difficult. In this blog post I hope to help you by discussing different types of backends and the technologies that you could use to build them.
Use this post as an inspiration to getting started and getting a direction for where to look next. What you need to learn next depends on the problem you are trying to solve. Don’t take everything that I’m saying as given but do some additional research of your own.
With all these frameworks and packages it is important that you have a good basic understanding of Python first. So if you’re totally new to Python take a look at some beginner courses before jumping to more advanced topics.
Website or a web app
It feels as if everything is a web app nowadays so it’s no wonder that Python provides the tools to build one. If you need to build a website with Python you could start investigating these frameworks.
Django
Django is one of the most popular web frameworks for Python. It has lots of features built right in such as user management, admin views, database connections, authentication, etc.
It has been used by companies like Instagram and Pinterest so its ability to scale has definitely been proved. And because of its popularity there is a market for professional Django developers.
Once you learn how to build applications with Django you can get results quite fast because of the rich feature set. Django is also very serious about security so you can rest assured that you’re probably not going to get hacked. It should be great for large projects.
Flask
If you need something simpler then you should give Flask a try. It is a micro framework with a relatively simple API, and it’s hugely popular because of its simplicity.
Flask can be the perfect choice for projects that don’t require all the fancy features that come with Django. Since it is a micro framework it tries to do one thing, developing web applications, really well. On the flipside you may need to do more work or find additional libraries when you want to add more features.
In theory you could build almost anything with Flask. In practice, it is better suited for smaller projects, and might be perfect for your next hobby project when learning Python.
Backend API
You might already have some other technology for your application or web interface and only need to build a backend API for handling the requests from your frontend clients. In this case you don’t need to use a framework with all the bells and whistles to build front ends.
Nothing prevents you from using Django or Flask for building APIs but here are some additional suggestions to take into consideration.
FastAPI
FastAPI is my current favorite framework for building APIs. It is performant and pleasant to work with. Also the documentation is really top notch.
FastAPI is built on top of Starlette which is a powerful ASGI framework. ASGI stands for Asynchronous Server Gateway Interface and it is a successor to WSGI (Web Server Gateway Interface) which standardized the interface between web servers and synchronous web applications. As you could probably infer from the name ASGI specifies the same interface for asynchronous applications.
It is still quite new and has a smaller community but I’ve been extremely happy using it and hope to see it gain traction. It is easy to learn and quite intuitive. Some of the syntax has been borrowed from Flask so you should be able to get going quickly.
Here are some highlighted features:
- high performance
- fun to code with
- automatic data validation with the help of Pydantic models and Python 3.6 type declarations
- autogenerated OpenAPI documentation
- all the features from Starlette
Starlette
I already mentioned Starlette which is a lightweight high performance ASGI framework.
While I’d recommend checking out FastAPI first you can by all means go with Starlette. In any case it is good to know its features because everything you learn there will more or less apply to FastAPI too if you choose to switch to it later.
And here’s what you get with Starlette:
- high performance
- WebSocket support
- GraphQL support
- background tasks
- CORS, GZip, Static Files, Streaming responses
- test client based on Requests
Tornado
If you need to scale to tens of thousand of simultaneously open connections you should take a look at Tornado.
Tornado is a web framework and asynchronous networking library. It has quite long history and it implemented asynchronous coroutines even before the async
and await
keywords were added to Python 3.
Some of its features include:
- asyncio integration
- asynchronous HTTP client
- native WebSocket support
- third-party login support
- unit testing for asynchronous code
You could also use the built-in templating feature to generate web pages.
Backend scripts
Enterprise backends often contain scripts that are triggered when some specific event happens, or they are scheduled to run at certain times. Some examples could be running weekly or monthly reports, handling new files or computing machine learning models.
A typical way to schedule scripts is to configure cron jobs on a Linux server. Cron is a system utility that will automatically run the configured commands at selected times. Because scripts usually need to be run with arguments you should also have an understanding of the built in argparse module, and check out the configparser module if you need to use configuration files.
Because of the large variety of the possible use cases it is difficult to say what other technologies you’ll need but here’s a list of possible packages that could be helpful to know:
- SQLAlchemy for communicating with databases
- Requests for communicating with web APIs
- Pillow for manipulating images
- NumPy for scientific computing
- Watchdog for monitoring file system events
And here are some additional modules from the Python standard library:
- tempfile for managing temporary files
- urllib.parse for URL manipulation
- csv for reading and writing CSV files
- json for encoding and decoding JSON
- ElementTree for parsing XML
- subprocess for running executables from Python
In the cloud
If you’re going to run Python in cloud environments you’re quite likely to do it with containers. Therefore it’s important to have an understanding of Docker and the way that containers work and how to build your own.
Kubernetes is the most popular platform for container orchestration but as a Python developer you generally don’t need to build your own cluster. Every major cloud provider nowadays has a managed Kubernetes service so you can concentrate on the actual coding instead of managing the platform yourself.
In the cloud you have the possibility to integrate with various services directly from code. For example Google Cloud Platform provides Python clients for each service and Amazon has boto3 to make use of AWS services. Using these services requires understanding of the platform se be prepared to wade through the corresponding documentation.
If you’re on AWS and you are using boto3 then you should also take a look at moto which is a library for mocking out AWS services in unit tests. There is no similar emulator available for GCP.
Wrap up
Hopefully this gave you some ideas on what to learn next on your journey to become a Python backend developer. As the scale of possibilities is so large it is difficult to give out a definitive list of things that you should learn. Still, I want to emphasize that you really should learn the basics of Python first.
Previous post
Get to the Correct Blogging MindsetNext post
CORS Simply Explained