Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Create and Deploy your First Flask App using Python and Heroku
Programming

Create and Deploy your First Flask App using Python and Heroku

Last Updated on August 27, 2020 by Editorial Team

Author(s): Divy Shah

Programming

Flask is a web application framework written in Python. Here I created an application using Python and deployed onΒ Heroku.

Click here to launch my demo webΒ app.

Prerequisites

Installation

After installing Python install the other frameworks and libraries listed. You can easily install flask using the following command.

pip install Flask
#pip install

Define the structure of your webΒ app

static
|_main.css
template
|_display.html
app.py
trending.py
requirements.txt

The static directory contains your CSS files, and the template contains the HTML file, which is used for rendering.

You can create a separateΒ .py file for your logic and other operations or write code in the same app.py I suggest creating a separate file will reduce the confusion.

Step 1:

Create your app.pyΒ file

I used the waitress service the reason behind using this library is meant to be a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones that live in the Python standardΒ library.

Install waitress

pip install waitress
app.py
import requests
from flask import Flask, render_template, redirect, url_for, request
from datetime import datetime, timedelta
import time
import json
import os
from trending import get_trending
app = Flask(__name__)
@app.route('/')
def trending():
info = get_trending()
render_template('display.html', info=info['data'])
return res
if __name__ == "__main__":
app.debug = False
port = int(os.environ.get('PORT', 33507))
waitress.serve(app, port=port)

As shown above, we used the GET Method to send data from theΒ server.

There are several other methods also available whichΒ are

POST: It is used to send user/form-data to the server doesn’t cache the transmitted data.

HEAD: It is similar to GET, but the difference is it is used without the responseΒ body

PUT: It is Used to replace the current resource with uploadedΒ content

DELETE: It is used to delete the target resource provided in theΒ URL.

Step 2:

Create trending.py file which is basically the file which contains your business logic after creation import main function in app.pyΒ file

from trending import get_trending

Below my trending.py file looksΒ like:

trending.py

After collecting the data, we can render the output using an HTMLΒ file

The output data will be displayed on the HTMLΒ page.

Step 3:

Create an HTML file for render the output which you collected from the responseΒ object.

Below is how my display.html file looksΒ like

display.html

You are almost done with the coding part, and now it’s time to deploy our first flaskΒ app.

Before deploying the app, first, check the flask app on the localhost.

Deployment Steps

  1. Login to your Heroku account usingΒ CLI

You can log in by writing the following command in the terminal.

heroku login

2. Create a web app onΒ Heroku

You can create a new application on Heroku using the following command

heroku create 

3. Create requirements.txt file in the same project directory

To generate requirement.txt file, you can use the following command

pip freeze > requirements.txt

4. Create aΒ Procfile

Procfile is a Process file which is required for all Heroku applications.

Procfile specifies the commands that are executed by the app onΒ startup.

Click here to read more onΒ Procfile

enter the following inΒ Procfile

web: gunicorn app:app

Here the app is the name of your main (.py) file in my case it isΒ app.py

if you don’t installed gunicorn web server then install it by using the following command

pip install gunicorn

NOTE: you have to create a Procfile without any file extensions

5. Create runtime.txt to specify the python version onΒ runtime

After creating a runtime.txt add your build python version likeΒ below

python-3.8.1

Now, we are allΒ set!

6. Initialize empty git repository and push theΒ code

git init

Next, is time to commit your final code using the following steps

git add .
git commit -m "My first commit"
git push heroku master

Your app is live Now you can see your web app using generated URL

Please check out my demo web application

https://trending-shows.herokuapp.com/


Create and Deploy your First Flask App using Python and Heroku was originally published in Towards AIβ€Šβ€”β€ŠMultidisciplinary Science Journal on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓