Skip to main content
|HUG|
=====

|PyPI version| |Build Status| |Coverage Status| |License|

Hug aims to make developing Python driven APIs as simple as possible,
but no simpler. As a result, it drastically simplifies Python API
development.

Hug's Design Objectives:

- Make developing a Python driven API as succint as a written
definition.
- The framework should encourage code that self-documents.
- It should be fast. Never should a developer feel the need to look
somewhere else for performance reasons.
- Writing tests for APIs written on-top of Hug should be easy and
intuitive.
- Magic done once, in an API, is better then pushing the problem set to
the user of the API.
- Be the basis for next generation Python APIs, embracing the latest
technology.

As a result of these goals Hug is Python3+ only and uses Falcon under
the cover to quickly handle requests.

.. figure:: https://raw.github.com/timothycrosley/hug/develop/example.gif
:alt: HUG Hello World Example

HUG Hello World Example
Installing Hug
==============

Installing hug is as simple as:

::

pip install hug --upgrade

Ideally, within a virtual environment.

Basic Example API
=================

happy\_birthday.py

::

"""A basic (single function) API written using Hug"""
import hug


@hug.get('/happy_birthday')
def happy_birthday(name, age:hug.types.number=1):
"""Says happy birthday to a user"""
return "Happy {age} Birthday {name}!".format(**locals())

To run the example:

::

hug -f happy_birthday.py

Then you can access the example from
localhost:8080/happy\_birthday?name=Hug&age=1 Or access the
documentation for your API from localhost:8080/documentation

Versioning with Hug
===================

versioning\_example.py

::

"""A simple example of a hug API call with versioning"""


@hug.get('/echo', versions=1)
def echo(text):
return text


@hug.get('/echo', versions=range(2, 5))
def echo(text):
return "Echo: {text}".format(**locals())

To run the example:

::

hug -f versioning_example.py

Then you can access the example from localhost:8080/v1/echo?text=Hi /
localhost:8080/v2/echo?text=Hi Or access the documentation for your API
from localhost:8080

Note: versioning in hug automatically supports both the version header
as well as direct URL based specification.

Testing Hug APIs
================

Hugs http method decorators don't modify your original functions. This
makes testing Hug APIs as simple as testing any other Python functions.
Additionally, this means interacting with your API functions in other
Python code is as straight forward as calling Python only API functions.
Additionally, hug makes it easy to test the full Python stack of your
API by using the hug.test module:

::

import hug
import happy_birthday

hug.test.get(happy_birthday, 'happy_birthday', {'name': 'Timothy', 'age': 25}) # Returns a Response object

Running hug with other WSGI based servers
=========================================

Hug exposes a ``__hug_wsgi__`` magic method on every API module
automatically. Running your hug based API on any standard wsgi server
should be as simple as pointing it to module\_name:\ ``__hug_wsgi__``.

For Example:

::

uwsgi --http 0.0.0.0:8080 --wsgi-file examples/hello_world.py --callable __hug_wsgi__

To run the hello world hug example API.

Building Blocks of a Hug API
============================

When Building an API using the hug framework you'll use the following
concepts:

**METHOD Decorators** get, post, update, etc HTTP method decorators that
expose your Python function as an API while keeping your Python method
unchanged

::

@hug.get() # <- Is the Hug METHOD decorator
def hello_world():
return "Hello"

Hug uses the structure of the function you decorate to automatically
generate documentation for users of your API. Hug always passes a
request, response, and api\_version variable to your function if they
are defined params in your function definition.

**Type Annotations** functions that optionally are attached to your
methods arguments to specify how the argument is validated and converted
into a Python type

::

@hug.get()
def math(number_1:int, number_2:int): #The :int after both arguments is the Type Annotation
return number_1 + number_2

Type annotations also feed into Hug's automatic documentation generation
to let users of your API know what data to supply.

**Directives** functions that get executed with the request / response
data based on being requested as an argument in your api\_function

::

@hug.get()
def test_time(hug_timer):
return {'time_taken': float(hug_timer)}

Directives are always prefixed with 'hug\_'. Adding your own directives
is straight forward:

::

@hug.directive()
def multiply(default=1, **all_info):
'''Returns the module that is running this hug API function'''
return default * default

@hug.get()
def tester(hug_multiply=10):
return hug_multiply

tester() == 100

**Output Formatters** a function that takes the output of your API
function and formats it for transport to the user of the API.

::

@hug.default_output_formatter()
def my_output_formatter(data):
return "STRING:{0}".format(data)

@hug.get(output=hug.output_format.json)
def hello():
return {'hello': 'world'}

as shown, you can easily change the output format for both an entire API
as well as an individual API call

**Input Formatters** a function that takes the body of data given from a
user of your API and formats it for handling.

::

@hug.default_input_formatter("application/json")
def my_output_formatter(data):
return ('Results', hug.input_format.json(data))

Input formatters are mapped based on the content\_type of the request
data, and only perform basic parsing. More detailed parsing should be
done by the Type Annotations present on your api\_function

**Middleware** functions that get called for every request a Hug API
processes

::

@hug.request_middleware()
def proccess_data(request, response):
request.env['SERVER_NAME'] = 'changed'

@hug.response_middleware()
def proccess_data(request, response, resource):
response.set_header('MyHeader', 'Value')

You can also easily add any Falcon style middleware using:

::

__hug__.add_middleware(MiddlewareObject())

Why Hug?
========

HUG simply stands for Hopefully Useful Guide. This represents the
projects goal to help guide developers into creating well written and
intuitive APIs.

--------------

Thanks and I hope you find *this* hug helpful as you develop your next
Python API!

~Timothy Crosley

.. |HUG| image:: https://raw.github.com/timothycrosley/hug/develop/logo.png
.. |PyPI version| image:: https://badge.fury.io/py/hug.png
:target: http://badge.fury.io/py/hug
.. |Build Status| image:: https://travis-ci.org/timothycrosley/hug.png?branch=master
:target: https://travis-ci.org/timothycrosley/hug
.. |Coverage Status| image:: https://coveralls.io/repos/timothycrosley/hug/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/timothycrosley/hug?branch=master
.. |License| image:: https://img.shields.io/github/license/mashape/apistatus.svg
:target: https://pypi.python.org/pypi/hug/

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hug-1.0.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hug-1.0.0-py2.py3-none-any.whl (27.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file hug-1.0.0.tar.gz.

File metadata

  • Download URL: hug-1.0.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hug-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7ed0563873b6c77a959e12b8cf3df53bc034aaff831c5a815e345d7d6897588f
MD5 72542bfa10c59ecf9e1368beb1e0dd02
BLAKE2b-256 34f3bd8ed085306c60fbe939da9030a7229e1ec8b570243383a6b034a7f24fee

See more details on using hashes here.

File details

Details for the file hug-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for hug-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ac18bd91a8fad3c6daf7c800b2ec776388aeef7f3e57f38e409d0cd697c10c82
MD5 2cbd9cf2bff92e33ec8a61d1f869fca5
BLAKE2b-256 c100e2f7d022640a7d3010a6323d90da6e149a4388835e55c763d60d70c6c788

See more details on using hashes here.

Release history Release notifications | RSS feed

2.6.1

2 files

2.6.0

2 files

2.5.6

2 files

2.5.5

2 files

2.5.4

2 files

2.5.3

2 files

2.5.2

2 files

2.5.1

2 files

2.5.0

2 files

2.4.8

2 files

2.4.7

2 files

2.4.6

2 files

2.4.5

2 files

2.4.4

2 files

2.4.3

2 files

2.4.2

2 files

2.4.1

1 file

2.4.0

1 file

2.3.2

1 file

2.3.1

1 file

2.3.0

1 file

2.2.0

1 file

2.1.2

1 file

2.1.1

1 file

2.1.0

1 file

2.0.6

1 file

2.0.5

2.0.4

1 file

2.0.3

1 file

2.0.2

1 file

2.0.1

1 file

2.0.0

1 file

1.9.9

1 file

1.9.8

2 files

1.9.7

2 files

1.9.6

2 files

1.9.5

2 files

1.9.4

2 files

1.9.3

2 files

1.9.2

2 files

1.9.1

2 files

1.9.0

2 files

1.8.2

2 files

1.8.1

2 files

1.8.0

2 files

1.7.1

2 files

1.7.0

2 files

1.6.5

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.1

2 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

This release

1.0.0 This release

2 files

0.2.0

2 files

0.1.0

2 files

0.0.8

2 files

0.0.7

1 file

0.0.6

1 file

0.0.5

1 file

0.0.4

0.0.3

1 file

0.0.2

1 file

0.0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page