You are on page 1of 23

ML CODE DEPLOYMENT

BITS Pilani
Pilani Campus
Objectives

● Get hands-on experience deploying the Flask framework in the

cloud
● Gain a fundamental working understanding of REST, CRUD

principles
● Apply said principles to deploy a solution

BITS Pilani, Pilani Campus


Introduction to Flask

• Developed by Armin Ronacher

• Lightweight and flexible web framework for Python

• Follows the WSGI standard for web server interaction


• Not a full-stack framework, allowing component flexibility

• Modular design encourages the use of extensions

BITS Pilani, Pilani Campus


Introduction to Flask (Contd.)

• Utilizes Jinja2 templating engine for dynamic content generation

• Built-in support for unit testing, RESTful request handling, and URL
routing

• Minimalistic core enables feature addition as needed

• Suitable for both beginners and experienced developers

• Widely used for web applications, RESTful APIs, and prototypes

• Installation via pip, application definition in Python script, and testing


with a development server

BITS Pilani, Pilani Campus


Flask - Routes
• In Flask, routes define how the application responds to
different URL patterns.

• Routes are specified using decorators in Python,


making it easy to map functions to specific URLs.

• Key Aspects of Python Routes:

• Decorator Syntax: Routes are created using the


@app.route decorator in Flask, where app is the Flask
application instance.

• URL Patterns: Define URL patterns that trigger specific


functions, allowing for dynamic content generation.

BITS Pilani, Pilani Campus


Flask - Routes (contd.)
• HTTP Methods and Routes:
• Default Method is GET: If not specified, the
route is associated with the GET HTTP
method.
• Multiple Methods: Routes can handle
multiple HTTP methods (e.g., GET, POST)
by specifying them in the decorator.
• Dynamic Routing and URL Parameters:
• Variable Parts in URL: Routes can include
variable parts, allowing for dynamic URL
patterns.
• Accessing Parameters: Parameters are
extracted from the URL and can be used in
the corresponding function.

BITS Pilani, Pilani Campus


REST

• REST Principles Overview:


• Representational State Transfer (REST) is an
architectural style for designing networked
applications.
• It emphasizes a stateless client-server
communication model, promoting scalability and
simplicity.
• Key REST Principles:
• Resource-Based: Entities (resources) are identified
and manipulated using URIs (Uniform Resource
Identifiers).
• Stateless Communication: Each request from a
client contains all the information needed to
understand and process the request. The server
does not store any client state between requests.
BITS Pilani, Pilani Campus
CURD
• CRUD Operations in REST:
• Create (POST): Introduces a new resource.
• Read (GET): Retrieves a representation of a
resource.
• Update (PUT/PATCH): Modifies an existing
resource.
• Delete (DELETE): Removes a resource.
• RESTful Request Handling:
• HTTP Methods: Correspond to CRUD
operations (e.g., GET for reading, POST for
creating).
• Status Codes: Indicate the success or failure of
a request (e.g., 200 OK, 404 Not Found).
• Stateless Communication: Enables scalability
and interoperability in distributed systems.
BITS Pilani, Pilani Campus
Jinja Templating

• Jinja is a templating engine used in Flask for dynamically generating


HTML, XML, or other markup languages.

• It allows embedding dynamic content within templates, making it


easier to create dynamic and data-driven web pages.
• Flask integrates seamlessly with Jinja, and templates are typically
stored in the /templates directory within the Flask project.

BITS Pilani, Pilani Campus


Jinja Templating (Contd..)

• Jinja supports template inheritance, allowing the creation of a base


template with common structure and placeholders for content. Child
templates then extend the base template and fill in the content.

BITS Pilani, Pilani Campus


Jinja Templating (Contd..)

• Key Aspects of Jinja Templating:

• Double Curly Braces Syntax: Variables and expressions are


enclosed in double curly braces {{ }} in Jinja templates.

• Control Statements: Jinja supports control statements, such as


loops and conditionals, using {% %} syntax.

BITS Pilani, Pilani Campus


Glossary of Terms

● HTTP (Hypertext Transfer Protocol):

● A protocol for transmitting hypermedia documents,

such as HTML, on the World Wide Web. It is the


foundation of data communication on the internet.

● JSON (JavaScript Object Notation):

● A lightweight data interchange format that is easy


for humans to read and write, and easy for
machines to parse and generate. It is commonly
used to transmit data between a server and a web
application, as an alternative to XML.
BITS Pilani, Pilani Campus
Glossary of Terms (Contd.)

● CSS (Cascading Style Sheets):

● A style sheet language used for describing the presentation of a

document written in HTML or XML. CSS describes how elements


should be displayed on the screen, in print, or in other media. It
enables the separation of content from presentation in web
development.

● SQL (Structured Query Language):

● A specialized programming language for managing and

manipulating relational databases.

BITS Pilani, Pilani Campus


Glossary of Terms (Contd.)

● WSGI (Web Server Gateway Interface):

● A specification for a universal interface between web servers and

Python web applications or frameworks, facilitating communication


and interoperability.

● REST (Representational State Transfer):

● An architectural style for designing networked applications, often


used in conjunction with HTTP, emphasizing a stateless client-
server communication model.

BITS Pilani, Pilani Campus


Glossary of Terms (Contd.)

● URL (Uniform Resource Locator):

● A reference or address used to access resources on the internet,

specifying the protocol, domain, path, and other parameters.

● URI (Uniform Resource Identifier):

● A string of characters that uniquely identifies a particular resource,

encompassing both URLs and URNs (Uniform Resource Names).

BITS Pilani, Pilani Campus


Glossary of Terms (Contd.)

● API (Application Programming Interface):

● A set of protocols, routines, and tools for building software and

applications, allowing different software components to communicate


with each other.

● PIP (Package Installer for Python):

● A package management system used to install and manage software


packages written in Python.

BITS Pilani, Pilani Campus


Glossary of Terms (Contd.)

● PyPI (Python Package Index):

● The official repository of Python software packages, where developers

can publish and share their Python libraries and packages for
distribution.

● SQLAlchemy:

● An open-source SQL toolkit and Object-Relational Mapping (ORM)


library for Python, providing a set of high-level API for interacting with
relational databases.

BITS Pilani, Pilani Campus


Glossary of Terms (Contd.)

● ORM (Object-Relational Mapping):

● A programming technique that converts data between incompatible

type systems in object-oriented programming languages, allowing


developers to interact with databases using object-oriented syntax.
SQLAlchemy is an example of an ORM library for Python.

BITS Pilani, Pilani Campus


ORM

• ORM in Python is a way to interact with


databases using a more natural, object-
oriented approach. Instead of writing raw SQL
queries, you can work with Python objects.

• Mapping Objects to Tables: Think of ORM


as a translator between your Python objects
and database tables. Each Python class
becomes like a blueprint for a table, and
instances of that class are like rows in the
table.

BITS Pilani, Pilani Campus


ORM (Contd..)

• Simplified Interaction: With ORM, you can perform database


operations using Python methods, making it more intuitive. For
example, instead of writing complex SQL to fetch data, you can use
Python methods to query and manipulate objects.

• SQLAlchemy is a popular ORM library in Python. It allows you to


define your database schema using Python classes and then
interact with the database using Python methods.

BITS Pilani, Pilani Campus


SQLAlchemy

• Object-Relational Mapping (ORM): SQLAlchemy


is a powerful Python library that provides an ORM,
allowing you to interact with databases using
Python objects. It bridges the gap between your
Python code and relational databases.

• Table Representations: In SQLAlchemy, Python


classes represent database tables, and instances
of these classes correspond to rows in those
tables. This makes it easier to work with databases
using familiar Python syntax.

BITS Pilani, Pilani Campus


SQLAlchemy (Contd…)

• Querying Simplified: Instead of writing raw SQL queries, you can


use SQLAlchemy query methods to retrieve, filter, and manipulate
data. It provides a high-level, Pythonic interface for database
operations.

• Database Abstraction: SQLAlchemy supports multiple database


engines, allowing you to switch between databases without
changing your code significantly. It abstracts away the differences
between databases, providing a consistent API.

BITS Pilani, Pilani Campus


Hands-on session

BITS Pilani, Pilani Campus

You might also like