Giter Site home page Giter Site logo

flask-app's Introduction

Example Flask app

This is an example Flask application that you will use to explore the CI/CD pipeline.

Init your development environment (on local computer)

Create a simple python application in your Visual Studio IDE and create a python script with the following code.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world! (v1)'

app.run(host='0.0.0.0', port=5000)

In this example, we create a Web Application using the Flask framework in Python. The script creates a server and demonstrates how to print a simple message on the browser.

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

Run the app

To run the script in your computer, you will need to install Flask:

pip install flask

Then, you can run it using the following command:

python3 app.py

How to containerise the app?

Before you continue, you must install Git on your local computer. Follow the guide below to download and install Git, then return to this tutorial.

For Windows users, please use the following step-by-step guide: https://phoenixnap.com/kb/how-to-install-git-windows

Install git bash and then restart your Visual Studio IDE.

Create a private repository and push it to GitHub following the commands below:

  1. Init an empty Git repository.
$ git init
  1. Add a new remote to your repo (this should be in one single line).
$ git remote add origin https://YOUR_GIT_USERNAME:YOUR_GIT_TOKEN@YOUR_GIT_REPO
  • Add all files and folders.
$ git add . 
  1. Commit the changes to the repo.
$ git commit -m "Create a sample web app"
  1. Upload local repository content to a remote repository.
$ git push -f origin master

At this point, refresh your GitHub page; your files/folders should be there now!

Now, go to your VM in GCP!

Deploy the web application on your GCP VM

  1. Open a terminal connection to the GCP VM. You can connect from VSC or using the SSH button (in GCP).
  2. In the VM, make sure you are already logged in as docker-user (from Lab 5.1).
  3. Let's clone our GitHub repo.
$ git clone --branch main https://YOUR_GIT_USERNAME:YOUR_GIT_TOKEN@YOUR_GIT_REPO
  1. Your repo should now be in your VM; run ls to check it out.
$ ls

flask-app
  1. Change the current working directory (move to the folder which contains the content of your repo).
$ cd flask-app
  1. Package your python scripts into a docker image following the set of if the Dockerfile.

The Dockerfile is used for specifying a set of instructions to follow in order to assemble an image.

Note: before building the image review the set of instructions in the Dockerfile using a text editor (e.g. pico)

# press ctrl+x to exit the editor
pico Dockerfile

Build the image now!

$  docker build -t flask .
  1. Run the container using the newly created image abd expose it to the port 5000.
$ docker run -d -p 5000:5000 flask

-d run process in the background.

  1. Try to access your service in the browser; you just created your first containerised app!:checkered_flag: Well done!

flask-app's People

Contributors

dkargatzis avatar steliosot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.