Artificial Intelligence, Programming

Deploying Algorithms on the Web with Flask and Heroku (for free)

Before stepping into deploying an AI model, let’s begin with deploying an algorithm

Michelangiolo Mazzeschi
Aug 8 · 10 min read
Image for post
Image for post
This is the final version, try it!

1. Creation of the Algorithm

def dice_roller():
import random
#roll dice
return int(random.random()*6)+1

2. Creation of the Graphic Interface

CSS part

Image for post
Image for post
Make sure you copy and paste them correctly in the editor, then click Run to see how the page will look.
<!DOCTYPE html>
<html>
<style>
html {
height: 100%;}
.wrapper {
margin-right: auto; /* 1 */
margin-left: auto; /* 1 */
max-width:700px; /* 2 */
height: 550px;
padding-right: 0px; /* 3 */
padding-left: 0px; /* 3 */
background-color: rgb(38, 38, 38);
border-radius: 25px;
text-align: center;
}
.text {
font-family: "Courier New";
#font-weight: bold;
#font-weight: 90;
font-size: 25px;
color: white;
}
.button{
margin:auto;
text-align: center;
display:block;
border-radius: 12px;
border-style: none;
font-size: 20px;
height: 50px;
width: 100px;
}
.submit{
margin:auto;
display:block;
border-radius: 12px;
border-style: none;
height: 150px;
width: 350px;
vertical-align: top;
text-align: center;
font-family: "Courier New";
font-size: 20px;
cursor: pointer;
}
/**
.
*/
}
}
</style>

HTML Part

<br></br>

<body class="wrapper" style="background: linear-gradient(#e66465, #9198e5)">
<div class="wrapper" style="background-color:rgb(255, 255, 255, 0.3)">
<div class="text">
<br></br>
<br></br>
Dice Roller by Michelangiolo Mazzeschi
</div>
<br></br>

<form>
<input class="submit" type="submit" value="Roll Dice" formmethod="post" style="background-color:rgb(208, 113, 133, 1); color: white; font-size: 30px;">
</form>

<br></br>
<br></br>
<div class="text" style="font-size: 60px;">
{{coin_output}}
</div>
</div>
</body>
<br></br>
<a href="https://medium.com/@ardito.bryan" class="text" >
Follow me on Medium
</a>
</html>

3. Creating your own app interface

Creating app.py

!mkdir templates
!mkdir static
#place your .html files in /content/templates
Image for post
Image for post
This is how your folders should like in your IDE.
#we cannot break the following paragraph
from flask import Flask, request, render_template
app = Flask(__name__)#questo è per runnarla su colab, nel deployment ricordarsi di commentarla altrimenti da errore immagino
!pip install flask-ngrok #only run the first time
from flask_ngrok import run_with_ngrok
run_with_ngrok(app) #starts ngrok when the app is running
@app.route("/", methods=['GET', 'POST'])
def home():
if request.method == 'POST':

def dice_roller():
import random
#roll dice
return int(random.random()*6)+1
#ad ogni refresh
#***l'html deve essere dentro la cartella templates per funzionare /content/templates/form1.html
return render_template('form1.html',
coin_output=dice_roller()
)
else:
#WE BEGIN FROM HERE
#***l'html has to be into the folder templates to start correctly /content/templates/form1.html
return render_template('form1.html',
coin_output='')
if __name__ == "__main__":
app.run()
#to export requirements.txt
#!pip freeze > requirements.txt
#!pip freeze

Explaining the code

@app.route("/", methods=['GET', 'POST'])
render_template(‘form1.html’,
coin_output=’’)

Running the code and see

Image for post
Image for post
This is how the output should be

In case of error

Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
File "/usr/local/lib/python3.6/dist-packages/flask_ngrok.py", line 70, in start_ngrok
ngrok_address = _run_ngrok()
File "/usr/local/lib/python3.6/dist-packages/flask_ngrok.py", line 38, in _run_ngrok
tunnel_url = j['tunnels'][0]['public_url'] # Do the parsing of the get
IndexError: list index out of range

A few attempts later…

Image for post
Image for post
First access to the web page by any user
if request.method == 'POST':
return render_template('form1.html',
coin_output=dice_roller()
)
Image for post
Image for post
After pressing the Roll button
#!pip install flask-ngrok
#from flask_ngrok import run_with_ngrok
#run_with_ngrok(app)

Download your work

Image for post
Image for post

4. Installing Tools: git and Heroku

Image for post
Image for post
Open cmd in your
C:\Users\Michelangiolo>git
C:\Users\Michelangiolo>heroku

5. Deploying the application using Command Prompt

cd ..
cd 'Folder Name'
C:\Users\Michelangiolo\Downloads\dice-roller>git init
C:\Users\Michelangiolo\Downloads\dice-roller>git add .
C:\Users\Michelangiolo\Downloads\dice-roller>git commit -m "X"
C:\Users\Michelangiolo\Downloads\dice-roller>heroku create dice-roller-ardito-bryan
C:\Users\Michelangiolo\Downloads\dice-roller>heroku git:remote -a dice-roller-ardito-bryan
>>> set git remote heroku to https://git.heroku.com/dice-roller-ardito-bryan.git
C:\Users\Michelangiolo\Downloads\dice-roller>git push heroku master
...
remote: -----> Launching...
remote: Released v3
remote: https://dice-roller-ardito-bryan.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/dice-roller-ardito-bryan.git
* [new branch] master -> master
Image for post
Image for post
https://dice-roller-ardito-bryan.herokuapp.com/

In case of fatal errors

heroku logs

Towards AI — Multidisciplinary Science Journal

The Best of Tech, Science and Engineering.

Sign up for Towards AI Newsletter

By Towards AI — Multidisciplinary Science Journal

Towards AI publishes the best of tech, science, and engineering. Subscribe with us to receive our newsletter right on your inbox. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanks to Towards AI Team

Michelangiolo Mazzeschi

Written by

AI Developer, open to job opportunities, https://www.linkedin.com/in/michelangiolo-mazzeschi-3a6a6881/

Towards AI — Multidisciplinary Science Journal

Towards AI is a world’s leading multidisciplinary science journal. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Michelangiolo Mazzeschi

Written by

AI Developer, open to job opportunities, https://www.linkedin.com/in/michelangiolo-mazzeschi-3a6a6881/

Towards AI — Multidisciplinary Science Journal

Towards AI is a world’s leading multidisciplinary science journal. Towards AI publishes the best of tech, science, and engineering. Read by thought-leaders and decision-makers around the world.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store