Chuyển tới nội dung
Home » Django For Data Science | Learning Curve

Django For Data Science | Learning Curve

Flask Vs Django and When Should You Use What?

Conclusion

In this tutorial:

  1. We used cookiecutter to jumpstart a Django project;
  2. We then trained a classification model based on 2000 movie review data;
  3. We created a local HTTP server to handle web traffic, taking a review text, and output a predicted sentimental analysis result
  4. We’ve accomplished a lot in this tutorial. If you have followed each step and successfully see a predicted result, you can proudly say now you have hosted your machine learning model and convert it into an HTTP service.
  5. In the next tutorial, we will walk you through Django for Data Scientists Part 2: Deploy A Machine Learning Model RESTFul API to the Cloud (Heroku) and let the whole internet use your machine learning service. Stay tuned.

We’ve created a crash course to teach you everything covered in this article with 1 hour long, 8 video lectures. Feel free to check the course here.

If you or your team has any questions about using Django for your machine learning service, please feel free to book a Django machine learning consulting with Leon here.

In the early 2000s, web programmers Adrian Holovaty and Simon Willison were working for an online newspaper called The Lawrence Journal-World.

Their mission was to develop a content management system (CMS) for the website. They began work on the project using the PHP programming language but soon encountered performance and security problems.

As an alternative, they decided to create their own Python framework. In homage to jazz guitarist Django Reinhardt, the project was named Django.

What is Django?

Django was released as open source in 2005. This Python tool aims to facilitate the rapid development of complex web applications while following best development practices.

Its main function is to relieve programmers of the most tedious tasks so that they can focus on writing applications using code and design patterns common to web development.

As a high-level Python web framework, Django enables the rapid development of secure, easy-to-maintain websites.

It makes it easy to build efficient websites by adding functions to support cache and database operations.

The special feature of this framework is that it covers both front and back ends. It allows you to manage databases and back-end functions as a Python tool, but also to edit files and create HTML pages as a front-end tool.

Thanks to its active community, Django has improved over the years and is backed up by extensive, detailed documentation.

It is one of the most widely used web development frameworks in the world, including famous applications such as Instagram, YouTube, and Spotify.

How does it work?

Every Django project consists of four main files that form the heart of the application. First, URLs.py is a URL mapping file that redirects HTTP requests to the view file based on the request URL.

The View.py file is responsible for processing HTTP requests sent by application users. These are usually Python functions or classes returning an HTTP response.

After receiving data from the model file, it sends responses to the template file for rendering. Views are linked to the application’s URLs, enabling access to the various functionalities.

The Models.py file defines the structure and management of the application’s database. Data models are the cornerstone of any Django application.

The framework provides an ORM (Object-Relational Mapping) that enables developers to work with databases easily and efficiently, without the need to write SQL queries.

This gives developers the ability to create data models using Python classes, which are then mapped to database tables.

Finally, the Templates.html file defines the presentation of the application’s user interface. These templates are used to display data taken from the database and processed by the views. This powerful system makes it possible to create dynamic, interactive user interfaces.

Advantages and disadvantages

The main advantage of Django is that it’s a complete framework that brings together everything a developer needs to build an application.

For example, you can use a ready-to-use website skeleton, form templates for collecting data, a user authentication system, and various tools for serializing XML and JSON data.

All these functionalities work together according to consistent design principles.

Many of these principles are similar to those of the Python language, such as concise, readable syntax. Others are specific to Django, such as consistency, rapid code expansion, and DRY: Don’t Repeat Yourself.

The latter implies that each feature must be in only one place to avoid redundancy. These principles make Django easy to use.

What’s more, this framework helps developers avoid the most common security problems, such as embedding session information in cookies.

Django can be used to build applications of any size, and scaling is very easy.

Debugging, extension, and maintenance processes are simple and systematic since all applications follow the same design principles.

However, this tool also has a few weak points. It’s not ideal for the development of basic applications and simple web interfaces, such as a landing page that doesn’t require interaction with a database or file directory.

Other options may be more suitable, as this is a complex, high-level framework with many features. A smaller framework such as Flask is preferable.

Similarly, Django is not the best choice for an application that is already large and cannot exist within a single code base.

The framework itself is rather large and can lead to a slowdown in the final application. Even if it enables rapid scaling of an application, the process itself may result in a slower application.

Finally, it’s best to avoid Django if you want to build every part of your project from the ground up and learn the basics of web development. This is because a large part of the process is automated.

Why learn Django?

More comprehensive than Flask, Django is also harder to learn. However, anyone who has mastered Python and the basics of web development will quickly be able to handle this framework.

Learning to use this tool can open many doors in your programming career. It’s a widely used solution for web development in Python, itself one of the most popular programming languages.

Many world-famous companies use Django to develop their applications, including Instagram, Disqus, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic, Open Knowledge Foundation, Pinterest, Spotify, and Open Stack.

Conclusion, Django: a simple web framework for Python programmers to master

Both simple and comprehensive, Django is used by many web developers to rapidly create feature-rich, extensible and secure applications.

Mastering this tool is essential in the modern web development. However, it is necessary to learn Python in order to use it to its full potential.

To discover this programming language, choose DataScientest. All our training courses include a module dedicated to Python and its main libraries, such as NumPy, Pandas, or SciPy.

Working with databases

Most web applications deal with data. Data is stored in databases, which are provided by different vendors. Let’s look at database support in Django and Flask.

Django has built-in Object Relational Mapping (ORM). ORM allows manipulating data in databases like objects in code. Django’s ORM supports PostgreSQL, MariaDB, MySQL, Oracle, and SQLite. Here’s what Django developers tend to choose, according to the Django Developers Survey 2022:

If you are going to store data in non-relational databases, like MongoDB or Redis, keep in mind that there is no native support for them in Django. According to the above survey, only 6% of developers use databases that are not officially supported by Django.

Another feature that facilitates database management in Django applications is the migration system. Django creates migrations automatically based on the changes you make to the application code, and then applies them to the connected database. Migrations are database-agnostic, can be put under version control, and allow for convenient rollbacks.

To work with migrations, the built-in manage.py utility is used. It provides a set of commands for managing migrations. PyCharm has a convenient manage.py console with code completion, which makes operations with migrations easier.

What can Flask offer? Nothing is built in, but virtually anything can be implemented. You can, for example, use SQLAlchemy, Flask-Peewee, or Flask-Pony for ORM, or store your data in NoSQL databases with Flask-PyMongo, Flask-Cassandra, or Flask-Redis.

Flask Vs Django and When Should You Use What?
Flask Vs Django and When Should You Use What?

Templating system: Django templates vs Jinja2 templates

If we only had to work with static HTML pages, that would be very easy, but most of today’s web applications include dynamic content. This is why we need a templating system.

Django has a built-in template engine, while Flask is fully compatible with Jinja2 templates.

Jinja2 was influenced by the Django template language. That’s why their syntax looks pretty similar. For example, both use double curly braces (

{{ }}

) for variables and curly braces with percent signs (

{% %}

) for logical elements, like loops and conditional statements.

At the same time, there are significant differences. Let’s look at these in more detail.

Compatibility

Django templates are tightly integrated with the Django framework. Some of their features, like template inheritance and template tags, are Django-specific.

Jinja2 is an independent template engine, compatible with various frameworks, including Django and Flask. That’s right: Although Django templates are the default choice for Django apps, you can use Jinja2 with Django, too! However, only 14% of Django developers do so, according to the Django Developers Survey 2022:

PyCharm has time-saving features for both template engines. For example, you can use gutter icons to navigate from templates to views, and vice versa – all in one click.

Flexibility and complexity

Jinja2 has a more complex syntax, while Django templates are less flexible and more restricted. There’s hardly any difference for basic applications, but you may face some limitations if you need to perform advanced operations in your templates.

For example, Jinja2 lets you define macros, which are reusable blocks of code:

{% macro greeting(name) %} Hello, {{ name }}! {% endmacro %} {{ greeting(“Alice”) }} {{ greeting(“Bob”) }}

Other examples of more flexible syntax in Jinja2 include:

  • Mathematical operations.
  • Built-in filters for string formatting and manipulation.
  • The ability to assign variables.

The functionality of Django templates has been intentionally restricted, but for good reasons, including:

  • Separating application logic from representation.
  • Security: Prohibiting arbitrary code execution in templates helps to prevent injection attacks.
  • Keeping templates accessible for non-programmers, such as designers.

Both template engines allow you to create custom tags and filters. If you’re using PyCharm, you can benefit from its support for custom template tags.

Extensions and customizations

Many limitations in Django templates can be compensated by using additional libraries. However, it’s important to remember that any extra dependency may negatively affect your application’s performance and security. Here are some popular extensions for Django templates:

  1. django-crispy-forms: introduces the

    |crispy

    filter and the

    {% crispy %}

    tag to let you format Django forms easily and beautifully.
  2. django-widget-tweaks: adds the

    {% render_field %}

    tag for customizing form fields by using an HTML-like syntax and a bunch of template filters for tweaking form fields and CSS classes.
  3. django-ckeditor: provides a WYSIWYG rich text editor so that your application’s users can add their own content as formatted text.

In Jinja2, you can also import extensions for the sake of customization or even more sophisticated logic. For example, Loop Controls adds support for

break

and

continue

in loops. The following loop will stop after processing 10 users from the

users

list:

{% for user in users %} {%- if loop.index >= 10 %}{% break %}{% endif %} {%- endfor %}

The Debug Extension lets you use the

{% debug %}

tag in Jinja2 templates to explore the context without setting up a debugger.

By the way, PyCharm lets you debug Django templates without any extra tags. Another great feature is the real-time preview of Django templates. The IDE renders templates as you edit them, so that you don’t need to switch to your browser.

URL dispatcher

URL dispatcher redirects incoming requests to specific views depending on the requested URL. Flask and Django handle this in different ways.

Routing

In Flask, routing is done by adding decorators to functions. Basically, here is how you create the application logic in Flask:

  1. Describe the desired behavior in a Python function.
  2. Decorate the function with

    @app.route

    .
  3. Specify the URL pattern in the parameter of the decorator (for example,

    @app.route(‘/’)

    ).

It’s as simple as that. Here’s the full code of a “Hello world” application in Flask:

from flask import Flask app = Flask(__name__) @app.route(‘/’) def hello_world(): return ‘Hello World!’ if __name__ == ‘__main__’: app.run()

Django uses dedicated Python files (usually, urls.py) in order to match the requested URL with the corresponding view (piece of application logic). Views are written separately in views.py. To show a page with “Hello World!”, your application must include the following files:

  • views.py: The app logic goes here.
  • urls.py: The routing is done here.
  • project’s urls.py: As Django is designed to have multiple applications in one project, you also need to include the URLs of the application in the project’s urls.py file.

To launch the “Hello World” application in Django, you also need to perform a few preliminary steps:

  1. Create a project.
  2. Create an app.
  3. Launch the Django server.

Thankfully, if you are working in PyCharm, all that is done automatically. For more information about creating Django projects in PyCharm, follow our Django app tutorial.

So, here is where Flask’s minimalistic approach shines. Its routing system is simple and intuitive, which perfectly fits small projects, especially learning ones. You can create your first Flask app in a matter of minutes, or use PyCharm’s Flask project template, which includes a sample “Hello World” application.

In complex cases, Django will be able to offer more powerful and flexible routing. To compensate for Django’s challenging routing system, PyCharm features a dedicated Endpoints tool window.

Handling URL parameters

URL parameters are variable parts of the URL that are used to send additional information to the application. Both Flask and Django support positional and named parameters, as well as type converters.

One advantage of Django is that it allows using regular expressions in URL patterns with the help of the

re_path()

function:

urlpatterns = [ re_path(r’^user/(?P

\w{0,50})/$’, views.user_details), ]

If you add such a pattern to your Django application, you’ll be able to request a URL, like

/user/john/

. The application will call the

user_details

function passing “

john

” as a parameter.

Compliance with RESTful principles

REST (Representational State Transfer) is an architectural style for web applications and services. Building web applications in accordance with REST principles is considered a best practice.

Flask enforces REST principles by design. It allows you to define routes for different HTTP methods separately.

In Django, views are associated with URL patterns regardless of the HTTP verb (GET, POST, PUT, DELETE, etc.). The subsequent differentiation is provided by the view, which is usually a class with the respective methods (

get()

,

post()

,

put()

,

delete()

, etc.):

This doesn’t mean that you can’t create a RESTful API in Django. Django REST framework is a package for developing web APIs that provides generic class-based views, a browsable API, serializers, and many other useful features. To build your first API in Django, follow our DRF tutorial.

Python Django Tutorial for Beginners
Python Django Tutorial for Beginners

Conclusion

At the end of the day, should you use Flask or Django?

It’s really difficult to say which Python web framework is better, let alone the best.

Both Django and Flask are equally suitable for many different tasks, but there are also so many aspects in which they differ. Every developer will make their own decision as to which framework to use, taking into account their skills, goals, and the nature of the projects they’re working on.

The table below summarizes all the points made in the article:

Django Flask
Templates Django template language: Jinja2:
URLs
Databases
Authentication and authorization Built-in apps: Extensions:
Testing
Architecture Perfect for microservices:
Learning curve

FAQ

Which is better: Django or Flask?

Both Django and Flask are modern, well-supported, and regularly updated frameworks. None of them is ‘better’, but you can choose which framework better suits your needs based on how complex your application or service will be, its architecture, the skills of your team members, etc.

If you are choosing your very first web framework for learning, you may want to start with Flask. It will be easier for you to learn Django afterwards.

Should I learn Django or Flask for a job?

Both frameworks are very popular, and you definitely need to know at least one of them to work in web development. However, learning both will help you land a job faster.

Is Django still relevant in 2023?

Yes. According to the Python Developers Survey 2022, Django was used by 39% of Python developers. The preliminary results of the Developer Ecosystem Survey 2023 show that Django’s popularity remains high at 40%.

Is Flask easier than Django?

Yes, from a learner’s perspective, Flask is a more accessible framework. A basic Flask application can be created in a single file in no time, whereas writing a “Hello World” in Django requires creating a few files and other preliminary steps.

At the same time, Django is a self-contained and well-documented framework, which means that you can use it to build a full-fledged project without having to choose and incorporate any extensions.

Is the Django framework the same as the Flask framework?

While both Django and Flask are popular choices for web development with Python, they are different in many aspects and each suits different use cases.

Django is generally used for larger, more complex projects that can benefit from its “batteries included” approach and numerous built-in features.

Flask is a good choice for simple applications or microservices. It’s a minimalistic framework that offers developers the flexibility to add the functionality they need.

Can you use Flask with Django?

Mixing Django and Flask in one application doesn’t make much sense, although it is technically possible. They have overlapping functionalities and often handle similar tasks in a different way. Bringing both Flask and Django into one project will cause unnecessary confusion and complexity.

However, combining Flask and Django can be justified in specific cases, for example, as you migrate your project from one framework to another. Another case is microservices that may require specific features, or be developed by teams with different skill sets.

Useful links

Documentation
  • Django documentation
  • Flask documentation
  • Jinja documentation
  • Django support in PyCharm
  • Flask support in PyCharm
Tutorials
Videos
Subscribe to Blog updates

Thanks, we’ve got you!

Authentication and authorization

Authentication means controlling who can access your web application, while authorization means providing specific permissions to those with access.

Django’s built-in authentication system handles both. It supports both users and user groups, and provides tools for granting and checking permissions. There are also a bunch of third-party packages for advanced authentication capabilities, including SSO, LDAP, and two-factor authentication.

Django also comes with an admin interface. The built-in admin app provides a ready-to-use interface for content management. After registering your models in admin.py, you’ll be able to perform CRUD (create, read, update, delete) operations on them.

A vast majority of developers who took part in the Django Developers Survey find admin and auth very useful:

To enable the admin app, add it to the list of installed apps in settings.py and provide a route for ‘

admin/

’ in urls.py. In PyCharm, Django admin is enabled by default and no additional steps are required. Just make sure that the Enable django admin checkbox is selected when creating your Django project.

Being inherently lightweight, Flask doesn’t offer any authentication or authorization features out of the box. However, there are extensions that can be integrated into your Flask application and work well together. For example, Flask-Admin provides an admin interface combined with ORM support, while Flask-Login and Flask-Security add the essential authentication features.

The downside of such an approach is that these extensions are not part of Flask and have their own release cycles, which may result in backward compatibility issues.

Flask Vs Django Web framework for Data Science | Flask vs Django | Machine Learning | Data Magic
Flask Vs Django Web framework for Data Science | Flask vs Django | Machine Learning | Data Magic

Instructor

We are providing online educational courses mainly about python / django by the community of web developers across the globe. Our classes are project based only, that means our methodology is based on “teaching by doing”. Discover with us: python, django, react js, javascript, machine / deep learning with python and much more!

In this article, I will explain to you how will we use Django Framework for creating some really exciting data science projects in Windows Machine. I will show you the complete procedure to create a website and also integrate a machine learning model into it. This website is used to check the quality of the Wine by providing some parameters.

Now, let’s talk something about Django, it is basically used to code the backend of a webpage and it is written in Python Programming language. This is used for fast website development, due to its huge support of python libraries. It was created by experienced developers to take care of most of the pain of the development of the website, allowing you to focus on building your app instead of reinventing the wheel. Django is free of cost and also available open-source.

1. How to make a virtual environment and use it in our project?

2. How to set up the Django backend for our project?

3. Training the machine learning model

4. Creating an application for that model

5. How to create the URL for our project (final output)

6. How to add the HTML templates to our application?

7. How to use CSS and apply images?

8. Finalizing the changes in our application

Let’s get started, 😀

We will firstly set up the Virtual Environment, by running that command in your windows command prompt-

1. Install Virtualenv (run the following command to install the virtual environment)

$ pip install virtualenv

2. Creating an environment (run the following command to create the virtual environment)

$ py -m venv DjangoMLProject

3. Activate it using that command (run the following command to activate that environment)

$ DjangoMLProjectScriptsactivate.bat

4. You can also deactivate it using (run the following command if you want to deactivate that environment)

$ deactivate

Now, we will see how to set up the Django framework for our project:

Firstly installing Django using pip within your virtual environment.

But be ensure that your virtual environment is activated.

$ py -m pip install Django

Creating your first Django project:

$ django-admin startproject WineMLProject

Now, move inside the WineMLProject directory and this type of directory structure appears to you:

WineMLProject/ manage.py WineMLProject/ __init__.py settings.py urls.py asgi.py wsgi.py

To verify that everything is working fine, you can also start the development server:

$ python manage.py runserver

The output of the command prompt will look like this:

Create a directory named as ML_Model/ in the main directory. Also, here we have another directory, named ML_Model/ directory that creates a python file named trainModel.py and pastes the following code given below. This file is used for training our model.

Also, download the file named data.csv from my GitHub link and paste it into the same directory. This file contains the data that will be used further for training.

Understanding Technicalities in our Machine Learning Model:

Import necessary libraries: Firstly, we import all the necessary libraries required to do the data processing (Pandas), make manipulations with matrix (Numpy), import machine learning model directly (Scikit-learn), etc.

Machine Learning algorithm: Here, we have used the Random Forest Machine learning algorithm, which gives better accuracy in terms of testing data. If you want to learn about the Random Forest algorithm, you can refer to the link given below.

Understanding Random Forest Regression in a detailed manner

Cross-Validation: Here we have used the stratified shuffle split technique for performing cross-validation, which is inbuilt in the sci-kit-learn. It is used mostly since it will resolve the problem of class biases while we split the given dataset into train and test split.

Finally, after the cross-validation, we can easily find the best accuracy from all the accuracies and that can be used for the final model formation, which we will be deploying on the server.

# These libraries are used by the machine learning model. import pandas as pd from sklearn.model_selection import train_test_split from sklearn.model_selection import StratifiedShuffleSplit from sklearn.ensemble import RandomForestRegressor import joblib # This is the data file, you can download this file from the github link provided. wine=pd.read_csv(“data.csv”) print(“Dataset Loaded…”) # Performing train test split with stratified shuffle split. train_set, test_set = train_test_split(wine, test_size=0.2, random_state=42) split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42) for train_index, test_index in split.split(wine, wine[‘Alchohol’]): strat_train_set = wine.loc[train_index] strat_test_set = wine.loc[test_index] wine = strat_train_set.copy() wine = strat_train_set.drop(“Wine”, axis=1) wine_labels = strat_train_set[“Wine”].copy() # Random Forest Regressor is used for prediction. model = RandomForestRegressor() model.fit(wine, wine_labels) print(“Model Training Ends…”) test_features=strat_test_set.drop(“Wine”, axis=1) test_labels=strat_test_set[“Wine”].copy() y_labels=model.predict(test_features) x=list(y_labels) y=list(test_labels) accuracy=[] for i in range(len(test_labels)): if x[i]>y[i]: accuracy.append((y[i]/x[i])*100) else: accuracy.append((x[i]/y[i])*100) joblib.dump(model, “rf_model.joblib”) print(“Model Saved…”) acc=sum(accuracy)/len(x) print(“Final Accuracy of the Model: “, acc)

The ML Model will be saved in your ML_Model/ directory, and this will look like this.

ML_Model/ trainModel.py data.csv rf_model.joblib

Now creating a separate app, in which we can start doing our work.

Write this command in your cmd, but be sure you must be in the main working directory.

$ python manage.py startapp WineApp

This will create a separate app, WineApp which looks like the following:

WineApp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py

Now, inside your WineApp directory, create a directory named templates this will store all HTML Templates, also create a directory named static that stores all static files like CSS/Javascript and Images.

Then, paste the following code into your views.py file.

# These libraries are used by Django for rendering your pages. from django.http import HttpResponse from django.shortcuts import render, redirect import joblib loaded_rf_model = joblib.load(“ML_Model/rf_model.joblib”) def index(request): if request.method == ‘POST’: # These are all the variables that we obtained from the user through a POST Request. alchohol_content=request.POST.get(‘alchohol_content’,’default’) malic_acid=request.POST.get(‘malic_acid’,’default’) Ash=request.POST.get(‘Ash’,’default’) alc_ash=request.POST.get(‘alc_ash’,’default’) Magnesium=request.POST.get(‘Magnesium’,’default’) Phenols=request.POST.get(‘Phenols’,’default’) Flavanoid=request.POST.get(‘Flavanoid’,’default’) NFPhelons=request.POST.get(‘NFPhelons’,’default’) Cyacnins=request.POST.get(‘Cyacnins’,’default’) Intensity=request.POST.get(‘Intensity’,’default’) Hue=request.POST.get(‘Hue’,’default’) OD280=request.POST.get(‘OD280′,’default’) Proline=request.POST.get(‘Proline’,’default’) labels=[[float(alchohol_content), float(malic_acid), float(Ash), float(alc_ash), float(Magnesium), float(Phenols), float(Flavanoid), float(NFPhelons), float(Cyacnins), float(Intensity), float(Hue), float(OD280), float(Proline) ]] # Now, predicting the quality of wine based on our parameters. our_labels = loaded_rf_model.predict(labels) if our_labels[0]<=400: wine_quality=”A Poor Quality Wine” if 400

1500: wine_quality=”A Premium & Fresh Wine” details={ “answer”:our_labels[0], “wine_quality”:wine_quality, } # Now rendering our results page with the data. return render(request,”results.html”,details) return render(request,”index.html”)

Now, go to WineMLProject/settings.py and follow that commands.

Register your templates/ directory inside the TEMPLATES, as shown in the figure below.

Register your app in the INSTALLED_APPS, as shown in the figure below.

Finally, paste this code to register the static/ directory.

import os STATIC_ROOT= os.path.join(BASE_DIR, ‘WineApp/static’) STATIC_URL = ‘/static/’

Go to WineMLProject/urls.py and paste that code. Also, be sure to remove all the code that is previously written in it.

from django.contrib import admin from django.urls import path, include urlpatterns = [ path(‘admin/’, admin.site.urls), path(”, include(‘WineApp.urls’)), ]

Now, go to WineApp/ directory, and create a file again named urls.py and copy the following code in it.

The above code will manage the URLs of your main webpage and the urls.py file created in WineApp/ directory will manage the URLs of your separate app.

from django.contrib import admin from django.urls import path from . import views urlpatterns = [ path(‘admin/’, admin.site.urls), path(”, views.index, name=”index”) ]

Inside your WineApp/templates/ directory create a HTML file named as index.html. This will contain your main index page which will be rendered first to the user.

Secondly, in the same directory, create an HTML file named results.html. On this page, the results of the quality of wine will be shown.

You can download both files index.html and results.html from my GitHub Repo.

Now inside your WineApp/static folder, copy the images and CSS files that are given in my GitHub Repo.

Finally, run that command in your command prompt to migrate all your changes.

$ python .manage.py migrate

Damm! You have created your first data science web app. Now you can run your app by starting the server.

$ python .manage.py runserver

This completes our today’s discussion on the project which involves Django, HTML, CSS with machine learning, and finally, we are come up with one of the exciting Data Science projects.

Here is my Linkedin profile in case you want to connect with me. I’ll be happy to be connected with you.

Here is my Github profile to find the complete code which I have used in this article.

Thanks for reading!

I hope that you have enjoyed the article. If you like it, share it with your friends also. Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion

Django vs Flask: Which is the Best Python Web Framework?

Testing

Testing is an integral part of professional web development. Let’s see what the most popular web frameworks have to offer us for testing our web applications.

Both Django and Flask have built-in testing support compatible with the native Python’s unittest module. They also provide a test client for sending HTTP requests to the application.

One of the few differences lies in handling the databases during testing. If your tests involve database operations, Django will create separate test databases for them. In Flask, developers need to manually ensure that their production database is not affected by tests. Third-party extensions, like Flask-SQLAlchemy, can help with that.

If you would like to benefit from advanced features of specialized testing libraries, such as pytest, you can use pytest-flask or pytest-django.

To test the API of your web service or application, try PyCharm’s HTTP Client. It lets you create and execute HTTP requests right in the code editor.

Introduction To Django Python Web Framework. When To Use Django OR When To Use Flask?
Introduction To Django Python Web Framework. When To Use Django OR When To Use Flask?

Description

Create professional and nicely looking User Interface in Django for Data Science with the use of Semantic UI framework. Understand the core concepts behind Pandas, Numpy, Matplotlib & Seaborn in order to create custom analytic tool i the browser for people who don’t know anything about programming. This unique course will introduce new concepts step by step and at the end the Student will have a very interesting position in the portfolio.

Architecture

As you may already know, there are two main styles in software architecture: the monolithic one and microservices.

Monolithic applications are ‘normal’ applications: They have a single codebase written in one programming language, are built and deployed as a single unit, and often have unified data storage.

Microservices architecture, on the other hand, involves developing a suite of small applications, where each unit is responsible for one thing and communicates with the others by means of an API. Every microservice has its own database, is deployed and scaled independently, and fails independently, without putting down the whole system.

Django encourages creating projects with several apps, where each app takes over a specific function. That might sound very similar to microservices, but it’s not. There’s still a single codebase and often a single database.

If you choose to develop with Django in PyCharm, you’ll always have access to a bird’s-eye view of the whole project structure in the Django Structure tool window:

Flask, the microframework, seems to be a perfect choice for microservices. With Flask, you can easily create a bunch of lightweight apps, empowering each with only the tools and extensions it requires. Full compliance with RESTful principles will also be a great help in establishing stateless connections between the microservices.

Django can be used in exactly the same way, although in this case the components of the system will be not so compact because of Django’s “batteries included” philosophy. But as long as you can use any stack for each particular microservice, you might as well develop a Django one when you need some Django-specific capabilities.

Django Project – Code a CRM App Tutorial
Django Project – Code a CRM App Tutorial

Conclusion

In this article, we covered one possibility of building a web app that can be used to automatically run a pipeline, as well as the dashboard component for clients. We also discussed modern techniques and tools that can be used with Python programming to achieve results.

The front end component can be built using Django with a combination of languages such as HTML (HyperText Markup Language), JavaScript and CSS (Cascading Style Sheets). Other frameworks such as React can also be used. This kind of backend can also be integrated with Microsoft PowerBI, with the help of an API. Although building the frontend is highly subjective, using Django templates for the frontend is useful as it can serve the purpose of building an all-inclusive web app. With this framework, the possibilities are endless. This architecture mostly helps to reduce coding, resources and maintenance, and facilitates reusable apps for similar projects. It consists of many non-isolated components which can be reused for other applications with the help of microservice architecture.

The Data Science Network article called Deploying Your Machine Learning Project as a Service expands on the practices relevant to project development we have mentioned. The techniques and concepts showcased in the article are also helpful during the various stages of project development and could be incorporated while deploying applications. Most of the resources are linked in the references.

For additional information, please contact us at [email protected].

Please take our quick 2-minute survey

We need your help to improve the Data Science Network. Please take 2 minutes to answer a few quick questions. We welcome and value your feedback. This is your last chance to complete the Data Science Network Newsletter feedback survey. Thanks to those who have already completed it!

Date modified:

Introduction

This tutorial will teach you how to productionize a machine learning model by serving it through a web API server with Django.

This is the first article for our Django for data scientist tutorials that aims to help a data scientist become more ‘full-stack’ and stand out among other data scientists.

We’ve created a crash course to teach you everything covered in this article with 1 hour long, 8 video lectures. Feel free to check out the course here.

Here are a few reasons to consider wondering how web development skills can help with your data science career.

  1. Web dev skills make your resume stand out among other candidates. It could be a killer skill for hiring managers who struggle to fight for engineering resources to productize a machine learning model;
  2. Very often, a decision-maker in a company is not very technical. If you can develop a prototype and allow them to interact with using a web browser, it gives you an edge to help them realize the power of your model and get green light your project;
  3. If you have a revolutionary idea about using AI to make a dent in the universe, you can quickly convert your idea to a web product, make it available over the whole internet, and let the whole world know about it.
Django Tutorial for Beginners | Full Course
Django Tutorial for Beginners | Full Course

Learning curve

Django is a complete framework, which results in a more challenging learning curve. On the other hand, you don’t need to learn anything besides Django. Everything a newbie usually needs, like ORM, authentication, authorization, and more, is already available in the main package, which comes with extensive documentation.

A Flask application can be created in seconds by writing just a few lines of code in a single file. So, if you are looking for a quick start, Flask may be a better choice. However, you have to be ready to explore extensions and other packages if you decide to develop your project further.

At the same time, Django projects tend to have more concise and consistent architecture. This results in shorter onboarding times when developers join already running projects.

From a team lead’s perspective, it’s also very important to take into account the needs and capabilities of your team.

Security

Security should always be considered when reviewing framework options. In this article, I talk about the Django Python framework and some of its packages. Django is a framework with many security features. It supports deployment behind https and encrypted sites using SSL (secure sockets layer). Django ORM (object-relational mapping) also protects from SQL injection-type queries where users can execute unwanted SQL code in the database. It also authenticates web addresses through an ‘ALLOWED_HOSTS’ value in the settings file, which contains the hostname of the webpage. This setting prevents cross site scripting attacks where fake hostnames can be used.

At Statistics Canada many of the Python web apps are deployed on the Advanced Analytics Workspace (AWW) which follows STATCAN guidelines for security, privacy and data confidentiality. Standard network protection elements like firewalls, traffic management, reduced user rights for application code and separation of concerns are all employed to reduce the overall attack surface for any potential vector. Further, RBAC (Role Based Access Control) is used to restrict access to the dashboard, preventing unwanted users from accessing apps. Various methods for scanning and vulnerability detection are also employed to ensure continued operation of secure applications.

Django 3 Course - Python Web Framework (+ pandas, matplotlib, & more)
Django 3 Course – Python Web Framework (+ pandas, matplotlib, & more)

Components

The components of the architecture and their functions are the database, the backend, pipeline, task scheduler and REST API (application program interface). I’ll elaborate on each of these components.

Database

A database collects and stores data. It’s the key component of a project, since all parts of a project are linked to the data. Databases can be classified as relational and non-relational databases. Relational databases can store data in a structured manner within tables and allow relationships with other tables. By default, Django supports relational databases such as PostgreSQL (structured query language), SQLite and MySQL. The choice of which to use depends on the type of project; PostgreSQL may be more suitable for larger projects, while SQLite may be more suitable for smaller projects such as those with fewer than 100,000 rows. MySQL is typically used when capabilities of PostgreSQL such as support for geospatial data or advance datatypes aren’t required. Non-relational databases do not use the format of a tabular schema to store data. Instead, they use object models specific to the type of data being stored. Elastic Search consists of a modern document-oriented database commonly used to quickly search millions of documents. Elastic Search could be used when the user needs to store an extensive amount of data and queries which need to be executed in real-time. In this project, I’ll use SQLite which is appropriate for the size of the task.

Backend

The backend is where most of the work is performed. Using Django as the backend framework facilitates the work based on improvements made during 15 years of steady development since its release on July 21st, 2005.Footnote 2 Django users can also benefit from a large online community. There are also many third-party packages available to suit a variety of tasks. In the following sections, I’ll present the packages and toolkits used to build an AIO web app for data science.

Pipeline

In the field of data science, a pipeline is a set of code or processes that convert raw data to usable data. Pipelines can sometimes be used to extract and structure data from a raw format while other times, they can use machine learning models to extract valuable insights from the data. There are many use cases for pipelines, but the selection of a pipeline depends on the requirements of the project. Often, projects in the domain of data science involve a set Python code that can be executed to perform some of the operations.

Task Scheduler

A task scheduler is used for performing asynchronous tasks that run in the background without halting other processes of the application. For time consuming tasks such as data pipeline processing, it is often preferred to handle these tasks in the background with the help of threads, without blocking the normal request-response flow.Footnote 3 For example, if your web application requires you to upload a scanned PDF document which needs to be pre-processed before invoking an extracting module, the user will view a loading page until the file is processed. Sometimes, the loading page can appear for longer than expected, and a timeout error will be received. This error occurs because browsers and web requests are not designed to wait endlessly for a page to load. This can be solved by pre-processing the PDF data in the background and sending the user a notification when the processing is complete. Task schedulers are a convenient way to resolve this behaviour.

By default, Django doesn’t support task scheduling, however, there are third-party Django apps such as Django-CeleryFootnote 4 that can integrate task queues asynchronously. This package can be installed with a pip install Django-Celery command. Extensive documentation is available for the library and can be referred to, when needed. A broker server such as Redis is required to facilitate communication between task scheduler and the backend. Redis stores messages produced by the Celery task queue application that describes the job at-hand. Redis is also used to store results from Celery queues, which can then be fetched from the frontend to present progress updates to the user. Some of the use-cases presented below can be supported by Django-Celery:

  • Running scheduled data retrieval cron jobs to update the database with the new data. (e.g., retrieve new data daily at 4 am).
  • Data cleaning and restructuring.
  • Running machine learning models whenever new data is ingested.
  • Updating database and reflecting changes on Dashboard.

Figure 2: Flow diagram of a task scheduler

Description – Figure 2: Flow diagram of a task scheduler

A process flow diagram of a task scheduler. Redis Server acts as a medium which can start tasks and keep track of the progress.Three boxes are at the top and are labelled as follows “Task 1 – Completed”, “Task 2 – Running” and “Task 3 – Running”, each individually point towards the lower box labelled “Redis Server / REST API”.

Figure 3: Task scheduler user interface

Description – Figure 3: Task scheduler user interface

A task scheduler user interface for the Frontend Dashboard. The table also shows useful information such as task name, status and success value. Action column contains a triangle ‘play’ button to execute a task; and a square ‘stop’ button to stop a running task.The tasks and their Status, Success and Action are as follows.Task 1 – Fetch New Data: Completed Status; Success is True and Action displays triangular white and blue play button.Task 2 – Clean PDF: Running Status; Success is False and Action displays black square stop button.Task 3 – Extract Data: Running Status; Success is False and Action displays black square stop button.

Running a pipeline this way is a better use of resources – pipeline tasks can be scheduled to run overnight when fewer users are on the app. By separating production tasks from development tasks also means that it develops efficiently. All distinct parts of the project can be combined in a single production-ready application. Finally, running the pipeline can provide users with additional control over the product since they can easily execute the pipeline as desired.

REST API

API’s are microservices that simplify software development. It’s often useful to separate different functions of an application into small web services. For example, when looking up an address using a postal code, a REST API for address lookup will return all street addresses within the postal code area. The response is often in JSON (JavaScript Object Notation) format which is an industry-standard and understood by many applications and programming languages. A REST API supports multiple methods, most commonly GET, POST, DELETE and UPDATE. Each method separates logic and helps structure code. These methods are self-explanatory and are responsible for getting, adding or posting new data, as well as deleting and updating data, respectively. Using REST APIs within web applications acts as a middleware between a database and a frontend – protecting the database from security vulnerabilities.

In Django, one can build APIs with a simple JSON HTTP response. However, there is a dedicated open-source toolkit known as Django-rest-framework which can be used for building APIs.Footnote 5 Some benefits of using Django REST framework include:

  • providing web browsable API out of the box,
  • Inbuilt Authentication policies for data security,
  • prebuilt serializers which can convert a Python object to a JSON object easily, and
  • extensive documentation, customizable and supported by various other Django plugins.

For use-cases similar to what’s presented in this article, the Django REST framework can be used for building data API’s and creating API’s for fetching tasks and pipeline progress while running in the background. This data can be integrated within the frontend to show the progress as illustrated in Figure 4.

Figure 4: Processing PDF Files

Description – Figure 4: Processing PDF Files

A GUI progress bar that shows the progress of a code that processes PDF files. The bar is at 70%. When a PDF file is processed, the slider size, percentage value and processed file count updates to show the status of the pipeline.The text at the top of the bar reads Executing PDF extraction pipeline, and the text below the progress bar reads 7/10 files processed. 2 minutes remaining.

Discover more

How to Connect Django With MongoDB

Learn how to use Django and MongoDB together to create a web application in PyCharm. Follow our step-by-step tutorial to connect MongoDB with Django.

Django vs FastAPI: Which is the Best Python Web Framework?

Introduction If you’re new to web development and have only recently heard of “Frameworks” as a method of speeding up your application development, then you’re in the right place. Today, we’ll explore the two most popular web frameworks in the Python ecosystem currently, namely Django and FastAPI…

PyCharm 2023.3: Feature Spotlight for Django Developers

PyCharm 2023.3 introduces a plethora of features for Django development: Django Structure tool window, Live preview for Django templates, enhancements in code assistance for the Django REST framework, and more! This release is poised to enhance the productivity of developers working with Django. Let…

This article was published as a part of the Data Science Blogathon.

We all know how popular the Python programming language is amongst Machine learning enthusiasts. So, once a machine learning model is ready, the next step is to deploy it to be used efficiently. But for deployment, there are various frameworks in Python that can be used. Flask vs Django is going to be an interesting comparison as both Python frameworks, and which one to choose for deployment is a good question. If you are also confused and stuck at the deployment stage, this article is for you. So, let’s deep dive and find out which is the best choice.

Image Source: https://programinghub.in

Choosing a Python Framework for model deployment depends on various criteria. We will go through each one by one. Firstly, let’s have a brief background to Flask and Django.

Flask is a micro web framework that is written in Python. Flask is very easy to learn, and also its implementation is straightforward. In just a few lines of code, you can get started with this. Flask is used in top tech companies also like: Netflix, Reddit, Mozilla, and so on.

Image source: https://dev.to

You can install this using pip: pip install flask.

With just this piece of code, you can get started:

from flask import Flask, render_template app = Flask(__name__)@app.route(‘/’) def home(): return ”

if __name__ == ‘__main__’:app.run(debug=True)

Features of Flask:

Refer documentation here: https://flask.palletsprojects.com/en/2.0.x/quickstart/

Django is also a web framework built on python. It’s open-source, accessible, and follows the MVC pattern(Model View Controller). Django provides excellent security features like CSRF, lightweight servers for dev and testing, etc. But since it has this MVC pattern, it confuses a lot of beginners. Instead, it is a perfect option for web development and deploying Machine learning models; many popular sites like Pinterest, Instagram, etc., are running on Django.

Image source: https://www.mattlayman.com/

Features of Django:

Refer documentation here: https://docs.djangoproject.com/en/4.0/

Head to head general comparison between Flask and Django:

Django Flask
It is a full-stack web framework and provides a lot of features. It is a lightweight framework with minimalistic features.
It comes with a ready-to-use admin framework that can be customized as well. No such feature here.
This framework ensures that developers use best practices because everything is template-based here. It is more open-ended, and developers don’t follow best practices here.
Django requires more lines of code. Flask required much fewer lines of code for the same task as Django.

Now, let’s discuss what should be selected from these two for your Machine Learning model deployment.

Some of the Machine learning models are very simply trained; for them using Flask is a good choice because Django is very much featured bulky framework, and hence not recommended for use with such models. If you compare lines of code in the application written in Django as compared to the Flask, Django will always have more lines. Django also has a proper folder structure and many libraries, making it unsuitable for small and simple machine learning models.

So, Flask is sufficient for almost all the machine learning models. If you have minor HTML code(around 100 lines) and smaller CSS code, you need to write very little regulation in Flask for model deployment. Django can also be used, but it will be complex and more suitable for those developers who are advanced in Python. Because that would require a lot of documentation reading, understanding the structure, and various other things, whereas, with Flask, there is no such headache.

Python is a straightforward and loved language, that comes with comprehensive and rich community support. Its syntax is concise, so a beginner does not face many issues while working with this. But instead of this, if you are not a master in Python, probably Flask is a good choice for you and will suit you more. But if you are a python expert, you might like Django more than the Flask.

Django comes with an integrated package for handling authorization and authentication. Django allows you to configure Users, Groups, Password hashing systems, and many other configurations. You can find these functionalities in the Django.contrib.auth module of Django.

But when we talk about Flask, there comes no built-in authorization and authentication, and you require an extension in the Flask for these things. Some extensions which are used in Flask to get this functionality are Flask-login, Flask-WTF, etc.

So, you can see that in both Flask and Django, you can get authorization and authentication. Django provides you with in-built things, whereas in Flask, you have to manage through extensions.

Every one of us, while developing something or writing programs, need help when we get stuck with bugs. Be it a pro or a newbie in coding; everyone needs community support at some point in time. And if you have solid and broad community support, life becomes much more accessible. So, when we discuss community support of both Flask vs Django, it is good, extensive, and knowledgeable. Django came into existence before Flask some five years ago. But instead of that, community support of both the frameworks is balanced and incredible for machine learning model deployment. You will most probably get answers to all your queries. So don’t worry about community support for both of them.

Form creation and handling in both frameworks are comparable. Both provide client-side validation as well as server-side validation. They both also handle common security threats. But the design of forms from models is dealt with by Django’s ModelForm, whereas Flask does have a native form handling feature, and Flask has to rely on the Flask-WTF extension. Django also protects cross-site scripting, forgery, and SQL injection. So, both of these almost provide the required things.

If you need to respond to an HTTP query or your model is small, and the codebase is light, go directly for a Flask-based application. But if you want to build something like next Facebook, Django will be a much better choice. If you have any confusion or doubt about which one to go for directly, go for Flask, it is straightforward, simple, and sure, it will get your work done. Django is also a fantastic package for building excellent applications, but it is confusing for beginners. For example, routing in the Flask is very easy, but that very same work in Django is a little bit complicated for beginners.

So, in this article, we had a look at Flask vs Django and which one is better for deploying machine learning models. So, now you can take your decision quickly.

I hope you find this article helpful. You can connect with me on blog.

Thanks for reading my article on Flask vs Django :).

Happy coding!

Want to read more articles on Python’s frameworks? Then, head on to our blog now.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

Thanks, I like Django better.

Write, captivate, and earn accolades and rewards for your work

Django with Data Science [Video]

Video $136.99

Subscription $15.99 $10 p/m for three months

What do you get with a Packt Subscription?

What do you get with a Packt Subscription?

What do you get with Video + Subscription?

What do you get with a Packt Subscription?

What do you get with eBook?

What do I get with Print?

What do I get with Print?

What do you get with video?

What do you get with video?

What do you get with Audiobook?

Video $136.99

Subscription $15.99 $10 p/m for three months

What do you get with a Packt Subscription?

What do you get with a Packt Subscription?

What do you get with Video + Subscription?

What do you get with a Packt Subscription?

What do you get with eBook?

What do I get with Print?

What do I get with Print?

What do you get with video?

What do you get with video?

What do you get with Audiobook?

  1. Free ChapterDjango Project

    • How to get Visual Studio code and Anaconda
    • Setting Up the Django Project Part I
    • Setting Up the Django Project Part II
    • Creating the First Model
    • Creating Another Model
    • Creating Our First View (with pandas Dataframes)
    • Merging Two Dataframes
    • Adding Static Files
    • Creating a Chart Selection Form
    • Adding JS to the Form
    • Sending the Data to the View
    • Displaying Error Messages
    • Adding Objects
    • Working with the Date
    • Performing GroupBy
    • Adding Additional Logic to the View
    • Chart Function Part I
    • Chart Function Part II
    • Chart Function Part III
    • Displaying the Chart
    • Styling the Error Message
    • Closing the Error Message
    • Creating Modal with Price Statistics
    • Adding Styling
    • Applying a Fix to the Chart and Button Creation
    • Creating Purchase View
    • Creating Django Model Form
    • Finishing Django Model Form
    • Testing the Model Form
    • Adding Send Confirmation
    • Navigation to the Purchase View
    • Remarks on the Chart View
    • Adding Navbar
    • Creating the CSV Model
    • Setting Up the Upload View
    • Creating the Django Form for File Upload
    • Resetting the Database
    • Chart View Fix
    • Exploring the CSV File – Sales Data
    • Saving CSV File via Form
    • Opening the CSV File – Sales Data
    • Transforming Rows of the CSV File – Sales Data
    • Populating the Database from CSV File – Sales Data
    • Creating Customers App and Model
    • Creating Customer View
    • Setting Up Customer View
    • Finishing the Customer View
    • Working on the Sales View Part I
    • Working on the Sales View Part II
    • Styling the Graphs
    • Creating the Home View
    • Creating Login Form and Login View
    • Working on the Login
    • Continue Working on the Login
    • Final Touches for the Login
    • Logout View
    • Outro
  2. Python Data Science Libraries

About this video

This course will show you how to create a professional and attractive user interface (UI) in Django for data science using the Semantic UI framework. If you don’t have a programming background, this course will help you create custom analytic tools in the browser by taking you through the core concepts of pandas, NumPy, Matplotlib, and Seaborn. With a step-by-step introduction to new concepts, this Django course will gradually help you get to grips with the essentials of data science. The code bundle for this course available at https://github.com/PacktPublishing/Django-with-Data-Science

Publication date:
August 2020
Publisher
Packt
Duration
7 hours 6 minutes
ISBN
9781800564725

Building an All-in-One Web Application for Data Science Using Python: An evaluation of the open-source tool Django

By: Nikhil Widhani, Statistics Canada

With the massive advancements in technology, many tools and techniques used in the data science community are slowly transitioning to open source. Open source tools are a no-cost solutions and the codebase can be modified to fit the needs of the user – providing them with flexibility and reducing costs. Proprietary tools typically get the work done efficiently, however, they can be costly and are still need to be adapted to the organization’s needs. Proprietary tools also often lack customizability and sometimes only solve parts of a problem. For more complex problems, like building a collection of microservices for a variety of tasks, it may require the use of multiple proprietary tools simultaneously.

Since open source tools give the user full control over the codebase, they allow organizations to apply security patches to new threats and maintain privacy and data confidentiality. Many open-sourced projects can be combined to form a one-point solution to serve a larger requirement.

Consider a project that builds a secure pipeline for data ingestion. Separate modules for this process could include regularly fetching new ethically-sourced data, running responsible machine learning algorithms on a timely basis, and building a dashboard for data visualization. In this article, I’ll explain the process of using a Django Web framework to create an AIO (all-in-one) production-ready web application that delivers information while protecting the privacy and confidentiality of the data it uses. This particular solution uses a dashboard for clients and an admin GUI (graphic user interface) for developers. The admin GUI can be used for running scheduled tasks at regular intervals to fetch new data, and to run a pipeline with the press of a button.

Django is an open-sourced web framework based on the Python programming language and is used by multinational corporations. Its core design supports flexibility, securityFootnote 1 and scalability. Apps built using Django can be reused for other projects without rewriting the code. At times, it’s as simple as copying the code from one project to another. To build this type of web app, we’ll use Django toolkits and plugins to develop a flexible architecture, while keeping scalability in mind. The proposed architecture is illustrated in Figure 1.

Figure 1: Proposed architecture for building an AIO web app.

Description – Figure 1: Proposed architecture for building an AIO web app

Proposed architecture for building an AIO web app. The Database works with the Django Backend (which includes the REST API (application program interface), Pipeline, Redis Server and Task Scheduler) and then onward to the Frontend, as developed by its users or administrator. Each component in this diagram are explained below.

CRUD mastery with Django | Build a CRM application | Django projects | #1
CRUD mastery with Django | Build a CRM application | Django projects | #1

Our Goals:

I will show you how to productize a machine learning model and create a web service hosting your model in 3 steps:

  1. Set up your Django project with Cookiecutter, a great tool to jump-start your Django project;
  2. Train a sentiment classification model using 2000 movie reviews data;
  3. Productize your model locally by setting up your web service API;

Let’s jump right in.

Creating a python virtual environment

  1. I am assuming you are using a Mac, and the same steps should apply to Unix/Linux based operating system;
  2. Follow the step by step instruction and install Django cookiecutter, a great Django package to jumpstart your project;


pip install "cookiecutter>=1.4.0"


cookiecutter https://github.com/pydanny/cookiecutter-django


Leons-iMac:projects leon$ cookiecutter https://github.com/pydanny/cookiecutter-django You've downloaded /Users/leon/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it? [yes]: project_name [My Awesome Project]: Classification Project project_slug [classification_project]: description [Behold My Awesome Project!]: My Classification Project author_name [Daniel Roy Greenfeld]: leon domain_name [example.com]: email [[email protected]]: [email protected] version [0.1.0]: Select open_source_license: 1 - MIT 2 - BSD 3 - GPLv3 4 - Apache Software License 2.0 5 - Not open source Choose from 1, 2, 3, 4, 5 [1]: 5 timezone [UTC]: US/Pacific windows [n]: n use_pycharm [n]: y use_docker [n]: n Select postgresql_version: 1 - 10.5 2 - 10.4 3 - 10.3 4 - 10.2 5 - 10.1 6 - 9.6 7 - 9.5 8 - 9.4 9 - 9.3 Choose from 1, 2, 3, 4, 5, 6, 7, 8, 9 [1]: 1 Select js_task_runner: 1 - None 2 - Gulp Choose from 1, 2 [1]: 1 custom_bootstrap_compilation [n]: n use_compressor [n]: y use_celery [n]: n use_mailhog [n]: n use_sentry [n]: n use_whitenoise [n]: y use_heroku [n]: y use_travisci [n]: n keep_local_envs_in_vcs [y]: n debug [n]: n [WARNING]: Cookiecutter Django does not support Python 2. Stability is guaranteed with Python 3.6+ only, are you sure you want to proceed (y/n)? y [SUCCESS]: Project initialized, keep up the good work!

Once the Django project is created, let’s create a virtual environment and use Python 3.6, so we can avoid conflicts by using the specific python libraries and


Leons-iMac:projects leon$ cd classification_project/ Leons-iMac:classification_project leon$ ls Procfile docs pytest.ini setup.cfg README.rst locale requirements utility classification_project manage.py requirements.txt config merge_production_dotenvs_in_dotenv.py runtime.txt Leons-iMac:classification_project leon$ virtualenv -p python3 venv Leons-iMac:classification_project leon$ ls Procfile docs pytest.ini setup.cfg README.rst locale requirements utility classification_project manage.py requirements.txt venv config merge_production_dotenvs_in_dotenv.py runtime.txt

Notice the newly created venv folder, which contains all the necessary files for your virtual environment.

Then we enter the virtual environment.


leons-iMac:classification_project leon$ source venv/bin/activate (venv) leons-iMac:classification_project leon$

Notice the venv in front of your shell prompt, which indicates that you are not in the virtual environment; leave the virtual env, deactivate on the command line.

Install all the Django libraries that are needed for your local dev environment


(venv) leons-iMac:classification_project leon$ pip install -r requirements/local.txt

Now start your Django development server,


(venv) leons-iMac:classification_project leon$ python manage.py runserver

And you will see this error message:


django.db.utils.OperationalError: FATAL: database “classification_project” does not exist

That is simply because Django tries to access the default Postgres database which does not exist yet. Let’s fix that.

(optional) If you have not installed Postgres on your computer, you can install it with homebrew.


mkdir homebrew && curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew

Then install Postgres


brew install postgres

Now that your Postgres server is installed, we can create a database for this project.


createdb classification_project

Then start the Django dev server again:


(venv) leons-iMac:classification_project leon$ python manage.py runserver Performing system checks... System check identified no issues (0 silenced). You have 23 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): account, admin, auth, contenttypes, sessions, sites, socialaccount, users. Run 'python manage.py migrate' to apply them. January 28, 2019 - 23:05:20 Django version 2.0.10, using settings 'config.settings.local' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

Open your web browser (e.g., chrome), then go to http://127.0.0.1:8000/

Viola, your Django website is initialed and up and running. Congratulations.

Now we run the migration command so that Django will create the first set of tables to provision the database.


(venv) leons-iMac:classification_project leon$ python manage.py migrate Operations to perform: Apply all migrations: account, admin, auth, contenttypes, sessions, sites, socialaccount, users Running migrations: Applying contenttypes.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0001_initial... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying users.0001_initial... OK Applying account.0001_initial... OK Applying account.0002_email_max_length... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying sessions.0001_initial... OK Applying sites.0001_initial... OK Applying sites.0002_alter_domain_unique... OK Applying sites.0003_set_site_domain_and_name... OK Applying socialaccount.0001_initial... OK Applying socialaccount.0002_token_max_lengths... OK Applying socialaccount.0003_extra_data_default_dict... OK

With the local Django dev project created, now we move on to build our model.

Since our main focus in this article is mainly about hosting a machine learning model, we will not go into too much detail about tuning the machine learning model parameters. Still, the same model serving method can also be applied to other models.

Start a Django app for modeling


(venv) leons-iMac:classification_project leon$ django-admin startapp modeling (venv) leons-iMac:classification_project leon$ cd modeling/ (venv) leons-iMac:modeling leon$ ls __init__.py admin.py apps.py migrations models.py tests.py views.py

After that, add ‘modeling’ to your installed apps in the project settings file: config/base.py.

Notice the app is currently located directly in our project root directory. Many of you may prefer to have a Django app inside the project_slug directory ( classification_project/classificaiton_project instead of classification_project/). To achieve that, follow these 3 simple steps:

1. move the entire app directory into classification_project/classificaiton_project/ and update the path.


mv modeling classification_project/ cd classificatino_project/modeling/

2. open the apps.py and change `name = modeling.app` to `name = “classification_project.modeling”`

If you followed the above step, make sure to include classification_project.modeling.apps.ModelingConfig to installed app section in the settings file: config/base.py

Now we need to install the scikit-learn libraries to train the model and predict an incoming sample.


(venv) leons-iMac:modeling leon$ pip install scikit-learn==0.20.2

We should also include scikit-learn in the requirements file to ensure it will be installed when deploying to production.


echo 'scikit-learn==0.20.2' >> requirements/base.txt

Now we can download the movie review data sets, which include 2 preprocessed data sets: positive reviews and negative reviews.


(venv) leons-iMac:classification_project leon$ cd modeling/ (venv) leons-iMac:modeling leon$ python Python 3.7.2 (default, Jan 13 2019, 12:50:01) [Clang 10.0.0 (clang-1000.11.45.5)] on darwin Type "help", "copyright", "credits" or "license" for more information.

Then execute the following script. The original script can be found on scikit-learn’s official GitHub page:

https://github.com/scikit-learn/scikit-learn/blob/master/doc/tutorial/text_analytics/data/movie_reviews/fetch_data.py


import os import tarfile from contextlib import closing try: from urllib import urlopen except ImportError: from urllib.request import urlopen URL = ("https://djangoml.s3-us-west-1.amazonaws.com/static/datasets/txt_sentoken.tar.gz") ARCHIVE_NAME = URL.rsplit('/', 1)[1] DATA_FOLDER = "txt_sentoken" if not os.path.exists(DATA_FOLDER): if not os.path.exists(ARCHIVE_NAME): print("Downloading dataset from %s (3 MB)" % URL) opener = urlopen(URL) with open(ARCHIVE_NAME, 'wb') as archive: archive.write(opener.read()) print("Decompressing %s" % ARCHIVE_NAME) with closing(tarfile.open(ARCHIVE_NAME, "r:gz")) as archive: archive.extractall(path='.') os.remove(ARCHIVE_NAME)

Now we exit the python and get back to the command line.


(venv) leons-iMac:modeling leon$ ls __init__.py apps.py model.file poldata.README.2.0 txt_sentoken admin.py migrations models.py tests.py views.py

There is a new folder txt_sentoken, which’s where 2000 preprocessed movie review files under two filer: pos (positive reviews) and neg (negative reviews).

Train the model and save the model into a pickle file. We relaunch python and paste the following code.

The original script and detailed explanations can be found here


import sys from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.svm import LinearSVC from sklearn.pipeline import Pipeline from sklearn.model_selection import GridSearchCV from sklearn.datasets import load_files from sklearn.model_selection import train_test_split from sklearn import metrics movie_reviews_data_folder = 'txt_sentoken' dataset = load_files(movie_reviews_data_folder, shuffle=False) print("n_samples: %d" % len(dataset.data)) docs_train, docs_test, y_train, y_test = train_test_split( dataset.data, dataset.target, test_size=0.25, random_state=None) pipeline = Pipeline([ ('vect', TfidfVectorizer(min_df=3, max_df=0.95)), ('clf', LinearSVC(C=1000)), ]) # for the parameters parameters = { 'vect__ngram_range': [(1, 1), (1, 2)], } grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1) grid_search.fit(docs_train, y_train)

Now that the model is trained using grid_search. We need to save the best model to serve the incoming requests and make predictions. Run the following in python:


from sklearn.externals import joblib joblib.dump(grid_search.best_estimator_, 'model.file', compress = 1)

You have now saved your classifier model into a binary file, ‘classifier.model’. That’s a lot of codes, stay with me, and we are almost there. Now let’s serve the model by creating a local API.

Open views.py from the modeling app, and add the following code.


from django.shortcuts import render import os from django.http import JsonResponse from sklearn.externals import joblib CURRENT_DIR = os.path.dirname(__file__) model_file = os.path.join(CURRENT_DIR, 'model.file') model = joblib.load(model_file) # Create your views here. def api_sentiment_pred(request): review = request.GET['review'] result = 'Positive' if model.predict([review]) else 'Negative' return (JsonResponse(result, safe=False)) Now that we have a predict function, we need to bind it to an URL, in the modeling folder, create a urls.py file and enter the following code: from django.urls import path from .views import api_sentiment_pred urlpatterns = [ path('api/predict/', api_sentiment_pred, name='api_sentiment_pred'), ]

Now we need to include this URL configuration in to project.

Open classification_project/config/urls.py file and add the following


# Your stuff: custom urls includes go here path('model/', include('classification_project.modeling.urls'))

Now let’s start the server from the project root, enter the following:


python manage.py runserver

After the Django server is up and running, it might take a few seconds for it to load the model. Go to your browser and enter the following URL:


http://localhost:8000/model/api/predict/?review=This movie is great

If everything is running as expected, you will see the predicted results says:

“Positive”

You can also try a few more examples such as:


http://localhost:8000/model/api/predict/?review=I really liked this movie


http://localhost:8000/model/api/predict/?review=This movie is long and boring

Alternatively, you can use cURL to submit a web request on your command line:


curl -G "http://localhost:8000/model/api/predict" --data-urlencode "review=This movie sucks"

Congratulations! Now you have successfully created a web server to host your machine learning model on your local machine. Read more on how to productionze this machine learning model and deploy it into a cloud service.

Overview

Even if you are new to web development, you probably already know that there are two main web frameworks in the Python world: Django and Flask. According to the Python Developers Survey 2022 conducted by JetBrains, 39% of developers reported using either or both.

Whether you are a beginner thinking of what to learn to get your first job in web development, a developer looking for a framework to build a web application on, or a team lead considering various technologies for an upcoming project, this article should help you make the right choice.

Before diving deep, let’s take a look at the basic principles and “philosophies” of Django and Flask:

  • Flask is a microframework, while Django is an “all-inclusive” one. This means that you have more flexibility in choosing the tools you want to use in Flask, whereas Django has more essential features available out of the box.
  • Django enforces certain requirements on the project layout, but for a good cause, as this encourages developers to create applications with a clean and pragmatic design. Flask provides more freedom, though this may result in longer onboarding times for new team members.
  • Flask and Django are both free and open source.
  • Both frameworks have a large community, detailed documentation, and receive regular updates.

Now that we’ve covered the essentials, let’s compare Flask vs Django by looking at the various aspects and challenges of using each framework for web development.

Vector Search RAG Tutorial – Combine Your Data with LLMs with Advanced Search
Vector Search RAG Tutorial – Combine Your Data with LLMs with Advanced Search

Keywords searched by users: django for data science

Django - Python Tutorial For App Development | Django For Data Science  Series | Part 1 - Youtube
Django – Python Tutorial For App Development | Django For Data Science Series | Part 1 – Youtube
Python And Django Project | Complete Python Project | Data Science Project  | Python Crm Project - Youtube
Python And Django Project | Complete Python Project | Data Science Project | Python Crm Project – Youtube
Part -1 | Django Machine Learning Project | Deploy Machine Learning Model  Using Django Framework - Youtube
Part -1 | Django Machine Learning Project | Deploy Machine Learning Model Using Django Framework – Youtube
Data Science Dojo On X:
Data Science Dojo On X: “💥 List Of Python Libraries And Frameworks:! Shared By: @Engr_Ali_Nawaz #Python #Datascience #Ai Https://T.Co/Dsxhbrliqz” / X
Videos | Django Feeds
Videos | Django Feeds

See more here: kientrucannam.vn

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *