In [10]:
import hug
import webcolors

In [11]:
@hug.get()
def hextoname(hex: hug.types.text):
    return webcolors.hex_to_name('#' + hex)

In [12]:
@hug.get()
def nametohex(name: hug.types.text):
    return webcolors.name_to_hex(name)

hug started from the command line in another terminal

hug -f embrace_hug.py -p 3000

In [13]:
!curl http://localhost:3000
# expect to get help because an undefined call was made


{
    "404": "The API call you tried to make was not defined. Here's a definition of the API to help you get going :)",
    "documentation": {
        "handlers": {
            "/hextoname": {
                "GET": {
                    "outputs": {
                        "format": "JSON (Javascript Serialized Object Notation)",
                        "content_type": "application/json"
                    },
                    "inputs": {
                        "hex": {
                            "type": "Basic text / string value"
                        }
                    }
                }
            },
            "/nametohex": {
                "GET": {
                    "outputs": {
                        "format": "JSON (Javascript Serialized Object Notation)",
                        "content_type": "application/json"
                    },
                    "inputs": {
                        "name": {
                            "type": "Basic text / string value"
                        }
                    }
                }
            }
        }
    }
}

In [14]:
!curl http://localhost:3000/nametohex
# expect to get error because name not supplied


{"errors": {"name": "Required parameter 'name' not supplied"}}

In [15]:
!curl http://localhost:3000/nametohex/?name=red 
# expect to get a hex color for red


"#ff0000"

In [16]:
!curl http://localhost:3000/nametohex/?name=limegreen 
# expect to get a hex color for limegreen


"#32cd32"

In [17]:
!curl http://localhost:3000/hextoname/?hex=32cd32 
# expect to get a word for the color


"limegreen"

In [19]:
!jupyter nbconvert --to pdf embrace_hug.ipynb


[NbConvertApp] Converting notebook embrace_hug.ipynb to pdf
[NbConvertApp] Writing 18711 bytes to notebook.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running pdflatex 3 times: ['pdflatex', 'notebook.tex']
[NbConvertApp] Running bibtex 1 time: ['bibtex', 'notebook']
[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations
[NbConvertApp] PDF successfully created
[NbConvertApp] Writing 57957 bytes to embrace_hug.pdf