Implement real-time updates with Django REST Framework
It is time to create another cool project using Django and REST Framework.
Watch YouTube tutorial: Implement real-time updates with Django REST Framework| Cryptocurrency API
In this post we are going to build real-time REST API.
Currently I am interested in cryptocurrency, so I decided to create Cryptocurrency API to use it in React. Well, we need to crawl and update data continuously and avoid from long request timeout.
Installation and Configuration
Let's start with creating new project named cryptocurrencytracking and inside your project create app named trackingAPI
and install REST Framework:
Once installation completed, open your settings.py and update INSTALLED_APPS.
So, I stated before we need to handle long term requests. Celery is the best choice for doing background task processing in the Python/Django ecosystem. It has a simple and clear API, and it integrates beautifully with Django. So, we are using Celery to handle the time-consuming tasks by passing them to queue to be executed in the background and always keep the server ready to respond to new requests.
To install celery run following command:
Celery requires a solution to send and receive messages; usually this comes in the form of a separate service called a message broker. We will be configuring celery to use the RabbitMQ messaging system, as it provides robust, stable performance and interacts well with celery.
We can install RabbitMQ through Ubuntu’s repositories by following command:
Then enable and start the RabbitMQ service:
Once installation completed, add the CELERY_BROKER_URL configuration to the end of settings.py file:
Then, create celery.py inside your project.
We are setting the default Django settings module for the 'celery' program and loading task modules from all registered Django app configs.
Now inside your __init__.py import the celery:
This will make sure our Celery app loaded every time Django starts.
In your models.py:
We are going to crawl website named Coinranking and if you visit the site you can see the field names there.
Crawling Cryptocurrency Data
We will use BeautifulSoup to crawl cryptocurrency values in given URL.
Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves the programmer's hours or days of work. Run the following command in your terminal to install beautifulsoup:
Now, create new file named tasks.py inside our app trackingAPI.
@shared_task will create the independent instance of the task for each app, making task reusable. This makes the @shared_task decorator useful for libraries and reusable apps since they will not have access to the app of the user.
As you see we are crawling our data and cleaning it from useless characters, then creating a new object in database.
Once data crawled we need contentiously update these objects.
As you see, we are crawling data every 15 seconds and updating our objects.
If you want to see the result, start celery in terminal:
and go check your admin to see created objects.
Alright! Now our objects are updating and we need to create API using REST Framework.
Now, create serializers.py inside our app.
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
The ModelSerializer class provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the Model fields.
For more information take look documentation
Next step is building API views, so open views.py:
and finally configure urls.py
when you run the server and celery (separate terminals) you will see following result:
Try to refresh the page every 15 second or every minute and you will notice that values are changing.
You can clone or download this project from my GitHub:Cryptocurrency-REST-API-Django
I hope you learned something from this tutorial and make sure you are following me on social media and please share Reverse Python with your friends.