Technical Thursday – Lightweight WebUI with Bottle for Python scripts

When you have many python scripts and several very useful feature inside them maybe you are thinking about it: whether can I make a WebUI over them? Then you realize you are not a web developer and you have never made WebUI. So you feel you have to spend hundreds of hours to learn web development or you should skip this option.

I was in this situation then I had found Bottle and I created a website over my python scripts. I know there are some other way to do it such as Django or Flask. For the very first usage the Bottle is a perfect choice.

In this article I would like to provide you a “getting started” guide for Bottle installation and configuration. (more or less this is a link collection)

 

Basics

When I started this I had three things in my mind:

  • I use Visual Studio for development
  • I would like to use it from Azure Web App
  • I would like to use it from a stand alone CentOS Linux

At first we are a very lucky situation because there are several article on Internet regarding this topic. Please kindly notes I am focus on Visual Studio related usage. πŸ™‚

 

Scenarios

1. Visual Studio and Azure

Here is a page for Visual Studio and python relationship: Python feature of Visual Studio

This is the easiest situation because we merely follow the instructions of this article: Publishing to Azure App Service

Then a page for Python web application project templates (this article contains that very useful information to install Python on App Service MS recommends using the site extensions. These extensions are copies of the official releases of Python, optimized and repackaged for Azure App Service.)

 

Note:
– Azure generally uses Windows machines under App services so your web application will be executed by an IIS
– You have to install the python extension under the App service where you would like to hosts your UI
– Bottle uses MVC modell

2. Visual Studio and CentOs

In this scenario we have to configure and prepare our server to bottle. Great thing…their is a very good article for this: How To Deploy Python Web Applications with the Bottle Micro Framework on CentOS 7

Here we need to be careful regarding the app.py related configuration because the original code is wrong for us.

Original code in app.py (service is not reachable from internet – only from localhost):

HOST = os.environ.get('SERVER_HOST', 'localhost')

Correct code:

HOST = os.environ.get('SERVER_HOST', '0.0.0.0')

Note:
– Bottle uses MVC modell

 

Tricks and Tips

Use git for code management

In the scenario 2 that is the most useful if you use a git for manage your code between Visual Studio and CentOs server

 

Modify ‘os.environ.get’ in ‘app.py’

Check the app.py related configuration because the original code is wrong for us.

Original code in app.py (service is not reachable from internet – only from localhost):

HOST = os.environ.get('SERVER_HOST', 'localhost')

Correct code:

HOST = os.environ.get('SERVER_HOST', '0.0.0.0')

 

‘reloader=True’ in ‘app.py’

Use ‘reloader’ inΒ  app.py because in this way after you pull the latest code to your CentOs, Bottle will reload automatically and use the latest code.

bottle.run(server='wsgiref', host=HOST, port=PORT, reloader=True)

 

Use different names for GET and POST

I suggest to use different names for GET and POST method related functions. I mean when you have an upload function you should use def upload():for GET and useΒ def do_upload(): for POST method in your code. Why? In this way you can call directly your upload (GET) function from another function.

 

Learn python code implementation

Finally you should improve your knowledge about existing python code implementation with Bottle: Building a Rest API with the Bottle Framework

 

Favicon 404

When you check the python console in debug mode you can see a 404 error for favicon:

"GET /favicon.ico HTTP/1.1" 404 743

You can manage this according to this article: Serve favicon with bottle.py

 

 

I think this is a very good fundamentals to make your codes available via browser… πŸ™‚

Be the first to comment on "Technical Thursday – Lightweight WebUI with Bottle for Python scripts"

Leave a comment

Positive SSL