Wednesday 27 June 2018

Python - Web - Visual Studio 2017 - Project Templates Surveyed

So this is Python month on this blog where I review Python technologies that might be of interest to Excel (VBA) developers. Web services are a way of exporting functionality to a wide range of clients including Excel VBA. So on the client side, VBA can call web services using the MSXML2.XMLHTTPRequest (XHR) class. On the server side can be any technology so long as it supports HTTP protocols.

VS2017 Python Web Project Templates Surveyed

In this post I look at the Visual Studio 2017 project templates catagorized as 'Python - Web' to see which is best for building a web service consumable from Excel VBA.

VS2017 Python Web Project Templates' Summaries

In Visual Studio 2017 when one select a new Project on has the following choices, selecting each one in turn one an read the template summary, I have deposited them here ...

Azure Cloud Service A project for creating a scalable service that runs on Microsoft Azure
Web Project A project for creating a generic Python web project
Bottle Web Project A project for creating an application using the Bottle web framework. It features sample pages that use the Twitter Bootstrap framework for responsive web design.
Django Web project A project for creating an application using the Django web framework. It features sample pages that use the Twitter Bootstrap framework for responsive web design.
Flask Web Project A project for creating an application using the Flask web framework with the Jinja template engine. It features sample pages that use the Twitter Bootstrap framework for responsive web design.
Flask/Jade Web Project A project for creating an application using the Flask web framework with the Jade template engine. It features sample pages that use the Twitter Bootstrap framework for responsive web design.
Blank Bottle Web Project A project for creating a Bottle web project.
Blank Django Web Project A project for creating a Django project.
Blank Flask Web Project A project for creating a Flask web project.
Polls Bottle Web Project A sample polls application using the Bottle web framework. It can be configured to use Azure Table Storage or MongoDB for storage.
Polls Django Web Project A sample polls application using the Django web framework.
Polls Flask Web Project A sample polls application using the Flask web framework. It can be configured to use Azure Table Storage or MongoDB for storage.
Polls Flask/Jade Web Project A sample polls application using the Flask web framework with the Jade template engine. It can be configured to use Azure Table Storage or MongoDB for storage.

VS2017 Python Web Project Templates' Feature Grid

It is probably better to see the difference between the templates with a feature grid ...

Blank Sample App (Polls) Framework Template Engine Bootstrap samples Azure Table Storage/MongoDB storage
Web Project
Bottle Web Project Bottle x
Django Web project Django x
Flask Web Project Flask Jinja x
Flask/Jade Web Project Flask Jade x
Blank Bottle Web Project x Bottle
Blank Django Web Project x Django
Blank Flask Web Project x Flask
Polls Bottle Web Project x Bottle x
Polls Django Web Project x Django x
Polls Flask Web Project x Flask x
Polls Flask/Jade Web Project x Flask Jade x

VS2017 Python Web Project Templates Glossary

And we ought to provide a glossary and links for some of those terms ....

BottleWikipedia: Bottle is a WSGI micro web-framework for the Python programming language.
DjangoWikipedia: Django is a free and open-source web framework, written in Python, which follows the model-view-template (MVT) architectural pattern
FlaskWikipedia: Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.
JinjaWikipedia: Jinja is a template engine for the Python programming language and is licensed under a BSD License created by Armin Ronacher.
JadeRenamed Pug. Jade/Pug is an HTML template engine
BootstrapWikipedia: Bootstrap is a free and open-source front-end framework (library) for designing websites and web applications.
Azure Table StorageMicrosoft: A NoSQL key-value store for rapid development using massive semi-structured datasets.
MongoDB storageWikipedia: MongoDB is a free and open-source cross-platform document-oriented database program.
WSGIWikipedia: The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language.

So Django is for a fully fledged web front end. Both Flask and Bottle are "microframeworks" which are lightweight compared to Django. Flask and Bottle seem quite similarly pitched, so which to choose? Well (at the time of writing) Flask has 22,409 StackOverflow questions and Bottle only has 1249 StackOverflow questions, making one metric in favour of Flask. One can review the battle of these frameworks with Google.

Blank Flask Web Project Web Service

So to see what one gets out of the box, selecting Blank Flask Web Project does some installs and gets a source file app.py which looks like this...

"""
This script runs the application using a development server.
It contains the definition of routes and views for the application.
"""

from flask import Flask
app = Flask(__name__)

# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app


@app.route('/')
def hello():
    """Renders a sample page."""
    return "Hello World!"

if __name__ == '__main__':
    import os
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT= 51382 #PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT)

Note, i have fixed the port number to be 51382. Running the project runs the Flask app in a development server ...

 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://localhost:51382/ (Press CTRL+C to quit)
127.0.0.1 - - [27/Jun/2018 18:21:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Jun/2018 18:21:14] "GET /favicon.ico HTTP/1.1" 404 -

and a new browser windows is spawned and navigates to the app's web address. The app is also callable from Excel VBA...

Sub TestCallFlask()
    '* Tools->References->Microsoft XML, v6.0
    Dim oXHR As MSXML2.XMLHTTP60
    Set oXHR = New MSXML2.XMLHTTP60
    
    oXHR.Open "GET", "http://localhost:51382/", False
    oXHR.send
    
    Debug.Print oXHR.responseText 'Prints Hello World!

End Sub

Links

Final Thoughts

Earlier examples in Python month show VBA<-->Python interop via a COM class but some return types (such as pandas' DataFrame) don't easily convert to something returnable through these classes (they often have to be convertible to a SAFEARRAY). Shipping your app as a web service can help bridge interop difficulties so web services are a valuable option.

No comments:

Post a Comment