Forums

hug.rest on PythonAnywhere, how to get started

Hello -

I'm looking at experimenting with http://www.hug.rest on PythonAnywhere. I think I need some tips or templates to get the web app part configured. I suspect I need to do the manual configuration when setting up a web app and to drop code into the WSGI file. What though I am having trouble with understanding. Also I am trying to run in a virtualenv.

Any help is much appreciated!

Hmm. hug.rest is down for me right now. Can't access it. What is it?

Hmm - web page is up for me.

http://www.hug.rest

Hug is a Python3 framework for creating APIs. The APIs can be exposed over HTTP, CLI, and locally.

Alright, got around to doing some experimentation tonight and I got a core example working. I'll quickly document what I did here for any others to get up and running.

First I setup folders/files in my main directory.

home    
    xxxxxx    
        app_hug
           app
               __init__.py
           venv

I created a python3 virtualenv into the venv folder.

Using the console with the venv activated I installed Hug. (note xxxxxx is your username)

pip3 install hug -U

I populated the init.py with some example code from the hug.rest Quickstart:

"""First hug API (HTTP access)"""
import hug


@hug.get(examples='name=Timothy&age=26')
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3):
    """Says happy birthday to a user"""
    return {'message': 'Happy {0} Birthday {1}!'.format(age, name),
            'took': float(hug_timer)}

Following that I jumped to the web tab on PA and setup a web application with Python3.5 and manual WSGi file. I pointed the source directory to my source. I pointed the virtualenv to the venv folder

/home/xxxxxx/app_hug/venv

Finally I edited the WSGI config file. It looks similar to the below after deleting out the comments).

# +++++++++++ CUSTOM WSGI +++++++++++
import sys

path = '/home/xxxxxx/app_hug'
if path not in sys.path:
    sys.path.append(path)

from app import __hug_wsgi__ as application

With all files saved, the final thing to do is to reload the web app. Once reloaded, you should be able to navigate to your URL and see a 404 page served with information on the rest API. You can query the example API too.

I'm just getting into Hug and learning it, so I haven't done any sort of load testing or performance checks. I'm glad I have PA to make it easy to serve and try this.

Cool, thanks for the write-up, @Awestruck