100 Scripts in 30 Days challenge: Script 9 & 10 — Create REST API and run any where with “hug”
Hi I learnt about hug python module from the below book, and initially I gave it a skip as there are many libraries around and I did not see the value of learning “hug” as already there was “Flask” & “Bottle” which I liked over something like “Django” .
But then while doing my 100 Scripts in 30 Days challenge I revisited the library and when I checked its capabilities, I thought it was impressive.
What I liked about Hug about the hug is if you write a API definition in Hug, you can use the same API from other Python Scripts or modules, you can run it as a command line application and also as a Web API. In all formats the documentation is self generated with examples.
The Code
- hug_intro.py covers the basic example of how to write an API using hug. In this example we are creating API to covert the color value between hex value and name.
- hug_versioned_api.py is a more involved example where we shows how we can create a versioned API using hug and how to run the program that runs as a command and as Web API. In this example we are using inflect module to convert words between singular to plural and vice versa. The difference between v1 & v2 is that v1 gives only lower case output and v2 gives output in same case as the input.
- Sample_Execution.txt gives examples of the execution that was done as a python module, command line application & Web API.
The code is give below.
For any feedback or inputs please contact me via twitter at:
Have a good day.