Home Machine Learning Constructing an Azure Container App : A Knowledge Analytics App with Python Flask, Plotly Sprint, and Docker | by Mert Ersoz | Feb, 2024

Constructing an Azure Container App : A Knowledge Analytics App with Python Flask, Plotly Sprint, and Docker | by Mert Ersoz | Feb, 2024

0
Constructing an Azure Container App : A Knowledge Analytics App with Python Flask, Plotly Sprint, and Docker | by Mert Ersoz | Feb, 2024

[ad_1]

Picture by Microsoft

Azure Container Apps present cost-effective, scalable, and absolutely managed app companies using a Kubernetes-based platform. A containerized software picture, comparable to one created with Docker, can simply be deployed with a minimal quantity of administration required to run the applying on the cloud. Many of the heavy lifting is managed by Azure, providing scalability at a manageable value.

The scalable value choice is good for intranet-based purposes the place a lot of the consumption happens internally, with customers interacting with the applying in a way just like how they might with PowerBI or Tableau companies. Nonetheless, not like PowerBI or Tableau, internet hosting a knowledge net software presents the benefit of not requiring person license prices, together with the flexibleness to beat limitations of those dashboarding instruments. When mixed with the convenience of improvement in Python, powered by frameworks comparable to Plotly Sprint, it presents a robust various to different dashboarding options.

Sprint Valuable Commerce App Screenshot by Tanya Lomskaya

Plotly is a well-liked knowledge visualization framework, accessible in a number of programming languages comparable to Python, R, JavaScript, and Matlab. Sprint, developed by Plotly, is a framework for constructing extremely interactive knowledge purposes. It employs a Python Flask server and makes use of React for constructing interactive net purposes, enabling customers to develop knowledge purposes completely in Python. Moreover, it presents the flexibleness to combine customized HTML/CSS/JavaScript parts as wanted.

Though options like Streamlit exist for constructing knowledge analytics purposes in Python, this text will particularly cowl Sprint as a consequence of its use of Python Flask for backend companies. Sprint’s utilization of Flask offers vital flexibility, permitting for varied purposes to coexist on the identical web site. For instance, sure sections of an app might be constructed utilizing pure HTML/CSS/JavaScript or React, whereas others would possibly incorporate embedded PowerBI studies.

Sprint is on the market in two flavors: open supply and enterprise. We are going to deal with the open-source model, addressing its limitations comparable to safety administration, which will be enhanced via using a customized Flask server.

Python’s Flask library is a light-weight but highly effective net framework. Its light-weight design is especially fitted to knowledge analytics professionals who could not have intensive net improvement expertise and like to begin with a minimalistic framework.

Flask additionally powers the backend server for Sprint. When builders create a Sprint software, a Flask server is mechanically initiated. Nonetheless, on this article, we’ll discover deploying a customized Flask server. This method maximizes the flexibleness of Flask, extending past using Sprint in our software. It allows the complete net software to make the most of different frameworks along with Sprint and facilitates the development of authentication pipelines. Such options will not be included within the open-source model of Sprint with out this customization however can be found within the Enterprise model.

Native Improvement Stipulations

There are a number of stipulations for organising the native improvement surroundings. Whereas not all of them are strictly required, it’s extremely really helpful to observe the rules offered beneath. It’s vital to notice that this tutorial was created utilizing a Home windows 11 machine. Some modifications could also be needed for different working programs.

  • Obtain Python. This tutorial makes use of Python 3.10.11. Its extremely really helpful so as to add Python to PATH throughout or after set up.
  • Obtain Azure CLI. This will likely be used to connect with Azure domestically by way of terminal.
  • Obtain Docker.
  • Obtain VS Code.
  • VS Code Azure Extension.
  • VS Code Azure Container Apps Extension.
  • VS Code Docker Extension.

App Construction

Beneath is the define of the demo app’s construction, designed to host a number of purposes throughout the net software, all powered by a Flask server.

AZURE_DATA_ANALYTICS_WEBAPP/
|---|essential.py
|---|figures.py
|---|property/
|---|static/
|------|css/
|------|js/
|------|img/
|---|templates/
|------|residence.html
|---|necessities.txt
|---|Dockerfile
|---|.dockerignore
|---|.gitignore
|---|.env
|---|.venv
|---|README.md

Python Native Surroundings Setup

We’ll start by making a Python digital surroundings named .venv within the VS Code terminal utilizing the instructions offered beneath. You’ll want to embrace .venv in each the .gitignore and .dockerignore information, as these shouldn’t be uploaded or deployed.

python -m venv .venv
.venv/Scripts/activate
pip set up --upgrade pip
pip set up -r necessities.txt
Picture by Creator

The necessities for the undertaking are listed within the necessities.txt file as follows:

# necessities.txt

# Net App Framework Requiremed Information:
Flask==3.0.2
plotly==5.19.0
sprint==2.15.0
gunicorn==21.2.0

# Azure required information:
azure-identity==1.15.0
azure-keyvault-secrets==4.7.0

# Different Utilities:
python-dotenv==1.0.1
numpy==1.26.4
pandas==2.2.0

Flask Server

The primary entry level to the applying will likely be essential.py, the place we’ll initialize and run the applying. On this file, we’ll outline the Flask server, which is able to function the backend server for the complete software.

At this stage, we don’t have any Sprint apps built-in; there’s only a single route outlined on the index web page of our software that renders the residence.html file. A route is a URL sample that handles and executes HTTP requests matching that sample. In our case, now we have a house route. Upon accessing the primary listing of the net software (“/”), the residence.html file will likely be rendered.

# essential.py

from flask import Flask, render_template

# Initialize Flask server:
# Let Flask know the place the templates folder is situated
# by way of template_folder parameter
server = Flask(__name__, template_folder = "templates")

# Outline Routes:
@server.route("/")
def residence():
"""
Index URL to render residence.html
"""
return render_template("residence.html")

# Run the App:
if __name__ == "__main__":
server.run(host = "0.0.0.0", port = 5000, debug = True)
# Set debug to False throughout manufacturing

<!-- templates/residence.html file -->

<!DOCTYPE html>
<html>
<head>
<title>Azure Knowledge Analytics Net Utility</title>
</head>
<physique>
<h1>Azure Container App Knowledge Analytics Utility
with Python Flask, Plotly Sprint & Docker
</h1>
</physique>
</html>

Observe that Flask expects HTML templates to be saved below the templatesfolder. Flask makes use of Jinja to parametrize the HTML templates.

We are actually able to run our software domestically for the primary time. Please observe that the debug mode is at the moment set to true, as proven above. Bear in mind to set this to false for manufacturing deployment. To run the app domestically, execute the command offered beneath after which go to http://localhost:5000/ in your browser.

python essential.py
Picture by Creator

We should always observe that when working domestically, as within the above instance, the applying doesn’t use HTTPS. Nonetheless, Azure Container App considerably simplifies improvement work by dealing with a lot of the heavy lifting associated to SSL/TLS termination.

It’s additionally vital to say that the native execution of the applying makes use of the default Flask HTTP server. For manufacturing deployment, nevertheless, we bind the applying to a Gunicorn server whereas creating the Docker picture. Gunicorn, a Python WSGI HTTP server for Unix, is healthier fitted to manufacturing environments. The set up of Gunicorn is included within the necessities.txt file.

Sprint App

Now, we’ll modify the essential.py file so as to add an occasion of a Sprint app. On this step, we initialize the Sprint app by offering the Flask server, specifying the bottom URL pathname for routing to the app, and indicating which property folder to make use of. The property folder can retailer pictures and customized type CSS information for the Sprint app.


# essential.py

from flask import Flask, render_template
from sprint import Sprint, html

# Initialize Flask server:
server = Flask(__name__, template_folder = "templates")

# Outline Routes:
@server.route("/")
def residence():
"""
Redirecting to residence web page.
"""
return render_template("residence.html")

# Sprint Apps:
app1 = Sprint(__name__, server = server,
url_base_pathname = "/sampleDashApp1/",
assets_folder = "property")

app1.format = html.Div([
html.H1("Sample Dash App"),
html.P("This is a simple Dash app running on a Flask server.")
])

# Run the App:
if __name__ == "__main__":
server.run(host = "0.0.0.0", port = 5000, debug = True)
# Set debug to False throughout manufacturing

Sprint helps most HTML tags, which will be specified straight in Python, as illustrated within the instance above. Up to now, we’ve added H1 header and P paragraph tags. Moreover, Sprint permits the addition of different parts, comparable to Sprint Core Elements. This function allows us to include widgets in addition to plots from the Plotly library.

Picture by Creator

Including Plotly Figures

We are going to outline some pattern Plotly charts within the figures.py file and subsequently incorporate them into the Sprint app.

# figures.py

import plotly.specific as px
import pandas as pd
import numpy as np

def line_chart():
"""
Pattern Plotly Line Chart
"""
df = pd.DataFrame({
"X": np.linspace(0, 10, 100),
"Y": np.sin(np.linspace(0, 10, 100))
})

fig = px.line(df, x = "X", y = "Y", title = "Line Chart")

return fig

def bar_chart():
"""
Pattern Plotly Bar Chart
"""

df = pd.DataFrame({
"Class": ["A", "B", "C", "D", "E"],
"Values": np.random.randint(10, 100, dimension = 5)
})

fig = px.bar(df, x = "Class", y = "Values", title = "Bar Chart")

return fig

If we modify the essential.py file to import bar_chart and line_chart from the figures.py file, we will then add these plots as graph parts to our Sprint app, as proven beneath.

# essential.py file

# ...Remainder of the code

from figures import line_chart, bar_chart

# Sprint Apps:
app1 = Sprint(__name__, server = server,
url_base_pathname = "/sampleDashApp1/",
assets_folder = "property")

app1.format = html.Div([
html.H1("Sample Dash App"),
html.P("This is a simple Dash app running on a Flask server."),
html.H2("Sample Line Chart"),
dcc.Graph(figure=line_chart()),
html.H2("Sample Bar Chart"),
dcc.Graph(figure=bar_chart())
])

# ... Remainder of the code

Picture by Creator

Constructing and Operating Docker Picture Domestically

We have now now established a skeleton code for our software and are able to construct our Docker picture. First, we have to allow virtualization from the motherboard’s BIOS settings, which is crucial for creating digital machines that Docker depends on. Secondly, the Docker software program and the VS Code Docker Extension have to be put in.

As soon as the above steps are accomplished, we create a Dockerfile in our essential listing and enter the next:

# Dockerfile

# Set Python picture to make use of:
FROM python:3.11-slim-bullseye

# Retains Python from producing .pyc information within the container:
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for simpler container logging:
ENV PYTHONUNBUFFERED=1

# Set up necessities:
RUN python -m pip set up --upgrade pip
COPY necessities.txt .
RUN python -m pip set up -r necessities.txt

# Set Working listing and replica information:
WORKDIR /app
COPY . /app

# Creates a non-root person with an specific UID
# and provides permission to entry the /app folder:
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

# Expose port 5000:
EXPOSE 5000

# Bind to make use of Gunicorn server
# and specify essential entry level essential.py/server object:
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "main:server"]

To construct the Docker picture, we execute the next command within the terminal:

docker construct --no-cache -t app:version1 .

With this step accomplished, we should always see a Docker picture created, seen within the VS Code Docker extension. On this occasion, now we have specified the title of the app as ‘app’ and the model of the picture as ‘version1’.

Picture by Creator

Now we’re able to run the app domestically. To create and begin an area Docker container, we execute the next command within the terminal:

docker run --env-file ./.env -p 5000:5000 app:version1

This can begin the Docker container domestically, permitting us to navigate to the app within the browser at http://localhost:5000/. Within the Docker run command talked about above, we instruct Docker to make use of the .env file situated in the primary listing because the environmental variables file. This file is the place we retailer app configuration variables and any secrets and techniques. It is essential to notice that the .env file have to be included in each .gitignore and .dockerignore information, as we are not looking for delicate data to be uploaded to Git or included within the Docker container. On Azure Container Apps, these parameters will be offered individually as environmental variables.

For our fundamental Azure deployment case, there is no such thing as a want to supply any environmental variables at this stage.

Picture by Creator

Beneath is the content material of the .dockerignore file:

**/__pycache__

**/.vscode
**/.venv
**/.env
**/.git
**/.gitignore

LICENSE
README.md

Now that now we have a working software domestically and have efficiently created a Docker picture, we’re able to arrange Azure and deploy the Container App.

To proceed with the steps outlined beneath, it’s essential to have the Azure CLI (a terminal software for speaking with Azure) put in, together with the VS Code Azure Container Apps extension.

Setup Azure Subscription

Picture by Creator

As soon as now we have created an Azure account, step one is to create a Subscription. A Subscription on Azure accommodates a set of Azure sources linked to a billing account. For first-time Azure customers or college students, there are credit accessible starting from $100 to $200. On this tutorial, nevertheless, we’ll deal with organising a paid account.

After signing up and logging into Azure, we navigate to ‘Subscriptions’, which will likely be tied to our billing account. Right here, we have to present a reputation for the subscription and proceed with its creation.

Observe: Particular care ought to be taken to observe billing, particularly in the course of the studying interval. It’s advisable to set budgets and alerts to handle prices successfully.

Picture by Creator

Setup Azure Useful resource Group

Azure manages a set of cloud sources below what is called an Azure Useful resource Group. We’ll have to create considered one of these teams to facilitate the creation of the mandatory cloud sources.

Picture by Creator

From the primary Azure web page, we will navigate to ‘Useful resource Teams’ to create a brand new one.

Picture by Creator

This Useful resource Group will likely be related to the Subscription we created within the earlier step. We might want to present a reputation for the Useful resource Group and choose a area. The area ought to be chosen primarily based on the closest geographical space to the place the applying will likely be primarily used.

Picture by Creator

Setup Azure Container Registry

Azure presents a container registry service generally known as Azure Container Registry, which permits us to host our Docker container pictures. This container registry serves as a repository for pictures, enabling model management over the container pictures. It additionally facilitates their deployment throughout varied cloud container companies, not restricted to Azure Container Apps alone. With this service, totally different variations of the app picture will be saved and deployed as wanted.

Picture by Creator

From the search bar within the Azure portal, we sort ‘Container Registries’ and click on the create button. This useful resource will likely be hosted below the Useful resource Group we created within the earlier step. We should always select the identical area as earlier than and choose the Primary plan choice for the bottom value.

There may be one vital setting that must be enabled on the Container Registry to permit us to deploy pictures to Azure Container App throughout the Azure portal. Go to the Properties of the Container Registry, allow the ‘Admin person’, after which save the adjustments.

Image by Author
Picture by Creator

Deploy Docker Picture to Azure Container Registry

Now that now we have our native Docker picture prepared and the Azure Container Registry setup full, we’re ready to add the container picture to Azure.

Within the VS Code terminal, we log in to Azure utilizing the Azure CLI (which have to be put in):

az login

This motion will open a pop-up window in your browser, prompting you to sign up to Azure. As soon as accomplished, we are actually authenticated with Azure.

Picture by Creator

The subsequent step is to log in to the Container Registry utilizing the terminal and Azure CLI. This may be performed by executing the next command:

az acr login --name <azureContainerRegistryName>
Picture by Creator

As soon as now we have efficiently logged in to the Container Registry, our subsequent steps are to first tag the picture after which push it to the Azure Container App.

Observe that ‘app:version1’ represents the title and model of our app, which we assigned when creating the Docker picture within the earlier steps.

docker tag app:version1 <azureContainerRegistryName>.azurecr.io/app:version1
docker push <azureContainerRegistryName>.azurecr.io/app:version1
Picture by Creator

With this step accomplished, we should always be capable of view the picture below the ‘Repositories’ part of the Container Registry, as described above.

It’s vital to notice that the above steps may even have been achieved utilizing the VS Code Docker Extension, as an alternative choice to using the Azure CLI.

Picture by Creator

Setup Azure Container Apps Surroundings & Container App

The Azure Container Apps Surroundings is a useful resource that may handle a set of container apps. Whereas we can’t create a Container Apps Surroundings straight, it may be arrange once we create an preliminary Container App.

To do that, we seek for ‘Container Apps’ within the Azure search bar and click on ‘Create’. Step one right here is to create a Container Apps Surroundings, as highlighted within the crimson sq. field within the picture beneath.

Picture by Creator
Picture by Creator

We choose the consumption plan to pay just for what we use and for offering a simplified setup course of.

After creating the Container Apps Surroundings, we return to the setup web page for the Container App to proceed constructing the app. Inside the Container setup web page, we choose Azure Container Registry and find the picture now we have beforehand uploaded.

Picture by Creator

For CPU and reminiscence, we go for the bottom quantity as this can be a demo app. If there are any environmental variables specified within the .env file for our native surroundings, we should enter them on this part. Alternatively, these environmental variables will be directed to Azure KeyVault.

We are able to skip the Bindings part for this tutorial and proceed to Ingress. On this part, we outline how Azure Container App handles visitors. We allow Ingress and set visitors to be accepted from anyplace if the applying is to be accessible on the net. For inner purposes, the corporate’s VNet ought to be used.

Moreover, we set the port of the applying to be 5000, which is similar because the Flask server’s port set throughout code improvement.

Picture by Creator

After configuring all of those settings, we’re able to create our software! Click on on ‘Overview + create’.

As soon as the setup is full, navigate to the Container App web page to search out the URL of the applying.

Picture by Creator
Picture by Creator

And that’s it! Our software is now up and working on Azure!

Handle Revisions

Now that now we have deployed the primary model of our software, we’ll cowl managing revisions.

As soon as now we have made adjustments to the code base and prepared with a revision, we run Docker deploy as earlier than with a brand new model title.

docker construct --no-cache -t app:version2 .

And add to Azure Container Registry:

az acr login --name <azureContainerRegistryName>
docker tag app:version2 <azureContainerRegistryName>.azurecr.io/app:version2
docker push <azureContainerRegistryName>.azurecr.io/app:version2

From right here on, now we have two choices. Both to make use of terminal and Azure CLI or go to Azure Container App web page and handle revisions from there. Right here we’ll cowl revision administration from Azure.

Picture by Creator

Hit “Create new revision” and supply a suffix to the brand new revision. Right here we’ll title it “version2”. Than click on on “app” check-box and choose edit.

Picture by Creator

In edit container part, choose the brand new model of the picture from Azure Container Registry and save. As soon as full, press create and new revision will likely be created.

Picture by Creator

After we come again to Revisions, we see the brand new container revision is taking 100% of the visitors. Though the outdated revision nonetheless stays and will be de-activated by way of Azure CLI.

Picture by Creator

We have now efficiently deployed a Container App to Azure utilizing Python Flask & Sprint. Nonetheless, this software is much from prepared for manufacturing launch.

For inner purposes, an authentication course of must be thought of. Utilizing Azure Energetic Listing and the Python MSAL library, it’s doable to outline roles and entry lists for customers inside an organization’s listing and management the authentication move with a Flask server. The Sprint Enterprise model allows this integration in a a lot simpler method. Nonetheless, the Enterprise model may not be an choice for everybody. We should remind once more that the selection of utilizing a customized Flask server in our Sprint software was made to beat the dearth of authentication help within the open-source model. Including Microsoft Energetic Listing authentication pipeline will likely be coated in a future article.

At this stage, the app is simply utilizing some demo plots and artificial knowledge. With the app being deployed on Azure, now we have many choices to serve our knowledge to the app. The primary choice is to make the most of Azure Knowledge Lake Storage for an economical file-based storage resolution. Whereas this selection offers storage at a really low value, it places the burden of computation on the net app. One other various is to make the most of Azure Databricks and its SQL Endpoint to dump heavy knowledge computations to a cloud cluster. These matters will likely be coated once more in one other tutorial.

  1. Overview of Python Container Apps in Azure — Python on Azure | Microsoft Be taught

[ad_2]