Convert python code as a web based service

Convert python code as a web based service

Scenario :

We have a simple python code which can be executed using a physical machine by developer or coder. However if you want extend this code a web based service where others can utilize the code using a web site.

Proof of concept :

Converting a simple python script which basically display a name and read text from a txt file.

Complete Source Code:

Download  Source Code :

Steps Involved:

  1. Take the code
  2. Separate the variables.
  3. Install flask
  4. Run the service
  5. Create html file which can collect the variables required.
  6. Create the route in app file to display html file
  7. Create the route in app file to collect the submitted data.
  8. Upload the content to a folder.
  9. Read the content of the text file uploaded.



  • Python installed with Anaconda command Prompt.
  • one text file named haneef.txt with some text for testing purpose.


Step 1: Basic Code

import os
file1 = open("haneef.txt","r+")
print("####### Hello Mr. Haneef #########")
print("Output of Read function is ")


Step 2 : Separate Variables.

import os

#Define Variables
person=”Mr. Haneef”

# Do not edit below this line
out1=”####### Hello “”+person+ #########”
file1 = open(filename,”r+”)
##### output is below this line
print(“Output of Read function is “)
##Note that all output is available at out1 and out2


Step 3 : Install Flask

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.

Read More about Flask :

conda install -c anaconda flask

Step 4. Test flask install

from flask import Flask, render_template, request
import os

app = Flask(__name__)

@app.route(“/”) # at the end point /
def hello(): # call method hello
return “Hello World!” # which returns “hello world”

@app.route(“/”) # at the end point /
def hello_name(name): # call method hello_name
return “Hello “+ name # which returns “hello + name

if __name__ == “__main__”: # on running python # run the flask app


Now save the above code as

Now run the server :




If you can see Hello World means Flask installation is successful.

Step 5 :

Identify the variables required from a user and create an html file to collect.

<h2>Sample Python Interface – Haneef Test</h2><br><br>
<form action = “http://localhost:5000/uploader” method = “POST”
enctype = “multipart/form-data”>
<br> Name of Uploader : <br>
<input type = “text” name = “person” />
<br><br><br>Select File : <br>
<input type = “file” name = “file” />
<input type = “submit”/>

Save above files as upload.html and save inside a folder called templates


  • person is a variable textbox input
  • file is a second variable which will allow to upload the file.


Step 6 :

@app.route(‘/upload’) # name of the route / end point user can access
def upload_file():
return render_template(‘upload.html’) # Name of html file in templates folder


Add above codes to as route and test the html.



Step 7 : Add route to collect submitted data

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_files():
if request.method == 'POST':
with open(p) as s:
# return 'file uploaded successfully'
return "Uploaded By: "+person.upper()+" :: "+file_content

Note : please add a variable called UPLOAD_Folder to define the upload path.

UPLOAD_FOLDER = ‘uploads/’

Also create a folder for storing uploaded files.


Save the file and open the route

Step 8 : Upload content and file through form created



Step 9 : Display the output

After submit you will receive the output as follows.


This is a proof of concept and can be extended to convert any python code as web service.

Download Complete Source Code :