Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Aug 23 01:42

    Peter200lx on develop

    Remove no-op comparison (.pop i… (compare)

  • Aug 17 22:21

    timothycrosley on develop

    Fix spelling error (compare)

  • Aug 11 06:06
    a523 commented #246
  • Aug 11 06:01
    a523 commented #246
  • Aug 11 05:52
    a523 commented #246
  • Jul 30 14:24
    teran-mckinney opened #823
  • Jul 28 20:19
    jay-tyler commented #822
  • Jul 27 18:55
    Annakan edited #822
  • Jul 27 18:53
    Annakan edited #822
  • Jul 27 18:53
    Annakan opened #822
  • Jul 27 16:35
    Annakan commented #319
  • Jul 25 03:24
    a523 commented #719
  • Jul 25 03:23
    a523 commented #719
  • Jul 25 03:22
    a523 commented #719
  • Jul 25 02:21
    a523 commented #719
  • Jul 25 02:17
    a523 commented #719
  • Jul 25 02:17
    a523 commented #719
  • Jul 25 02:10
    a523 commented #719
  • Jul 24 09:49
    a523 commented #719
  • Jul 24 09:48
    a523 commented #719
Jackemuk
@jackemuk
Thanks Tim!
Alexander Sukhoverkhov
@naryl
Hello. I can't find hug's API docs. There are none on the official site, and none on readthedocs. Google only finds the official website too. So, where do I get documentation on everything hug can do?
William Moskal
@wmoskal
Hi, noob here with a noob question - For my co-op we are using hug for our API, and i am having some trouble with getting a file, specified by a file selector from the HTML page, to be uploaded to a directory of my choice. How could I do this with Hug?
This is nowhere near enough! https://github.com/hugapi/HOPE/blob/master/all/HOPE-257-Documentation-Guide-for-Hug-Code.md has been accepted, but not yet implemented, and includes a requirement for comprehensive browsable documentation
Timothy Edmund Crosley
@timothycrosley
@wmoskal https://github.com/hugapi/hug/blob/develop/examples/file_upload_example.py provides an example of accepting a file from a hug endpoint, you can then write it to a file using standard Python file writing libraries (with open(file_destination): ...)
@/all https://github.com/hugapi/HOPE/pulls there's a bunch of open HOPEs that I would appreciate feedback on. These will form the basis of the next Hug release which will begin development June 15th. Thank you!
William Moskal
@wmoskal
Thanks Timothy, I got that working, but I now have another question. When I am using the CLI feature from hug, it is not passing the args that I am inputting on the command line, rather my hug path ....local/bin/hug, despite me entering a filename. Do you have any idea why this might be? (code below) @hug.cli()
def getStudentInfo(filename:hug.types.text, studentID:hug.types.number):
'''Returns student information given a filename and student ID'''
classList = getClassListData(filename)
studentInfo = studentID + ': ' + classList[studentID][0] + ',' + classList[studentID][1] + ',' + classList[studentID][8]
otherInfo = 'Term: ' + classList[studentID][4] + ', Course: ' + classList[studentID][3]
otherInfo += ',Section: ' + classList[studentID][2] + ',Program: ' + classList[studentID][5]
otherInfo += ',Level: ' + classList[studentID][6] + ',Rev: ' + classList[studentID][7]
return {'Student Info': studentInfo, 'Other Info':otherInfo}
William Moskal
@wmoskal
as a side note, I have attempted to copy and paste the CLI example, happy_birthday and it is still returning the hug path, instead of what I am inputting via the command line
William Moskal
@wmoskal

The file path that is being used instead of the args that I am passing contains #!/usr/bin/python3

-- coding: utf-8 --

import re
import sys

from hug import development_runner

if name == 'main':
sys.argv[0] = re.sub(r'(-script.pyw?|.exe)?$', '', sys.argv[0])
sys.exit(development_runner.hug.interface.cli())

Timothy Edmund Crosley
@timothycrosley
@wmoskal I believe you may have ran into this issue: hugapi/hug#808
If so updating hug to the latest version should resolve it for you
Hug 2.5.5 has been released:
    Fixed issue #808: Problems with command line invocation via hug CLI
    Fixed issue #647: Support for arbitrary URL complexity when using CORS middleware
    Fixed issue #805: Added documentation for map_params feature
    Added initial automated code cleaning and linting partially satisfying HOPE-8 -- Style Guideline for Hug
    Implemented HOPE-20 -- The Zen of Hug
For an Easter Egg on the new release, try from hug import this
William Moskal
@wmoskal
Thank you!!!! That was the issue!
William Moskal
@wmoskal
Hi Timothy, is there a way to 'call' a get request from the body of a function that was routed as a post request, and is there a way to do so with passing variables?
For example,
I upload a file, and after that I want to go directly to the index page, and on the index page i want to show a summary of what the uploaded file was, any errors, etc.
Timothy Edmund Crosley
@timothycrosley
Hi @wmoskal, Yes! There are a few ways to accomplish this depending on the exact behaviour you are looking for
I've going ahead and added an example that runs through the options: https://github.com/hugapi/hug/blob/develop/examples/redirects.py
William Moskal
@wmoskal
Thank You!
Timothy Edmund Crosley
@timothycrosley
No problem! Glad to help!
William Moskal
@wmoskal
Hi Timothy, sorry another question. Is there a way to create a splash page, ie a page that lasts for 5 seconds and then returns to the main page.
William Moskal
@wmoskal
I think i got it with pure html. <meta http-equiv="refresh" content="5;url=index.html" /> But it would still be good to know if there is a way to do this with hug. Thanks again!
Timothy Edmund Crosley
@timothycrosley
I think you did it the right way! IMHO HTML, or other client side approaches, are the correct answer here :)
William Moskal
@wmoskal
Hi! I was just wondering if hug offers any support for visual representations of data, such as graphs, and if not, if you had any recommendations for python packages that could do this? Thanks!
Timothy Edmund Crosley
@timothycrosley
Hi @wmoskal, hug doesn't have anything built in, but supports easy utilization of any of the existing libraries
I've added an example, for instance, of utilizing matplotlib: https://github.com/hugapi/hug/blob/develop/examples/matplotlib/plot.py
William Moskal
@wmoskal
Thanks!
William Moskal
@wmoskal
Not a big thing, more just curious if hug has any support for it, but does hug have any way to accept command line arguments for parameters that are named? Currently if i have
hug.cli()
def hello(first='William', last='Moskal', age=19):
print("hello" + first + " " + last + ", " + str(age))
this will give hello William Moskal, 19 even if I had previously done
William Moskal
@wmoskal
hug -f pythonFile.py -c hello Timothy Crosley <age> (your age)
or hug -f pythonFile.py -c hello first="Timothy" last="Crosley" age=<age>
William Moskal
@wmoskal
Hi, When I upload a microsoft excel file (.xlsx), to a hug endpoint, the file becomes corrupted. Is this a hug error, or a server error, or a html error, or some other error?
x10an14
@x10an14
@wmoskal Perform a sha256sum before and after, and at the server-location where it's uploaded?
William Moskal
@wmoskal
@x10an14 Thanks! I'll try that out
Micahj
@micahj
Hey @timothycrosley you are a legend. Hug is awesome
Timothy Edmund Crosley
@timothycrosley
@micahj Thanks for the kind words! Glad you like hug!
Zeethulhu
@Zeethulhu
Hi everyone, could someone please advise how I can return the URL of another route in hug?
for example;
import hug

users = [{"user_id" : 5432, "given_name" : "John", "surname" : "Smith", "pic" : "JSmith.png"},
         {"user_id" : 2345, "given_name" : "Jane", "surname" : "Doe", "pic" : "JDoe.png"}]

@hug.get('/profile', examples='user_id=5432')
def getProfile(user_id : int):
    user = next((i for i in users if i['user_id'] == user_id), None)
    return user


@hug.get('/photo', output=hug.output_format.image("png"), examples='user_id=5432')
def servePhoto(user_id : int):
    user = next((i for i in users if i['user_id'] == user_id), None)
    return user["pic"]


# hug -f profiles.py
# http://localhost:8000/profile?user_id=5432
#
# current output:
# {
#  "user_id": 5432,
#  "given_name": "John",
#  "surname": "Smith",
#  "pic": "JSmith.png"
# }

# desired output:
# {
#  "user_id": 5432,
#  "given_name": "John",
#  "surname": "Smith",
#  "pic": "http://localhost/photo?user_id=5432"
# }
Jackemuk
@jackemuk
Can someone point me to where I can find how to manipulate the documentation output format? Where do I change the "404" message? I tried to change the format to yaml using hug_yaml, but sample information on the pypi web page is different than the github page, and neither seems to work for me. Is there a way to filter the endpoints? For example, all endpoints that start with /assets* and only the GET methods? Any help to point me in the right direction would be great, Thanks!
x10an14
@x10an14
@Zeethulhu:
#!/usr/bin/env python3.6

import json

from falcon import HTTP_422   # Pick your poison: https://stackoverflow.com/a/10323055
import hug

users = [{"user_id" : 5432, "given_name" : "John", "surname" : "Smith", "pic" : "JSmith.png"},
         {"user_id" : 2345, "given_name" : "Jane", "surname" : "Doe", "pic" : "JDoe.png"}]


@hug.get('/test')
def testy(request):
    from IPython import embed
    embed()
    return


@hug.get('/profile2')
def get_profile(request, response):
    try:
        user_id = int(request.params['user_id'])
        assert(
            any(user_id == u['user_id'] for u in users)
        )
    except (KeyError, TypeError, AssertionError):
       response.status = HTTP_422
       response.body = "This URL must be called with a 'user_id={user_id}' parameter with a valid user_id."
       return

    user = next((i for i in users if i['user_id'] == user_id), None)

    (base_url, _) = request.url.split(request.path)
    user['pic'] = f"{base_url}/photo?user_id={user_id}"
    return user


@hug.get('/profile', examples='user_id=5432')
def getProfile(user_id : int):
    user = next((i for i in users if i['user_id'] == user_id), None)
    return user


@hug.get('/photo', output=hug.output_format.image("png"), examples='user_id=5432')
def servePhoto(user_id : int):
    user = next((i for i in users if i['user_id'] == user_id), None)
    return user["pic"]


# hug -f profiles.py
# http://localhost:8000/profile?user_id=5432
#
# current output:
# {
#  "user_id": 5432,
#  "given_name": "John",
#  "surname": "Smith",
#  "pic": "JSmith.png"
# }

# desired output:
# {
#  "user_id": 5432,
#  "given_name": "John",
#  "surname": "Smith",
#  "pic": "http://localhost/photo?user_id=5432"
# }
[2019-08-17 19:06:37] 0 x10an14@machine:~/random
-> $ pipenv graph
hug==2.5.6
  - falcon [required: ==2.0.0, installed: 2.0.0]
  - requests [required: Any, installed: 2.22.0]
    - certifi [required: >=2017.4.17, installed: 2019.6.16]
    - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
    - idna [required: >=2.5,<2.9, installed: 2.8]
    - urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.3]
ipython==7.7.0
  - backcall [required: Any, installed: 0.1.0]
  - decorator [required: Any, installed: 4.4.0]
  - jedi [required: >=0.10, installed: 0.15.1]
    - parso [required: >=0.5.0, installed: 0.5.1]
  - pexpect [required: Any, installed: 4.7.0]
    - ptyprocess [required: >=0.5, installed: 0.6.0]
  - pickleshare [required: Any, installed: 0.7.5]
  - prompt-toolkit [required: >=2.0.0,<2.1.0, installed: 2.0.9]
    - six [required: >=1.9.0, installed: 1.12.0]
    - wcwidth [required: Any, installed: 0.1.7]
  - pygments [required: Any, installed: 2.4.2]
  - setuptools [required: >=18.5, installed: 41.1.0]
  - traitlets [required: >=4.2, installed: 4.3.2]
    - decorator [required: Any, installed: 4.4.0]
    - ipython-genutils [required: Any, installed: 0.2.0]
    - six [required: Any, installed: 1.12.0]
[2019-08-17 19:06:41] 0 x10an14@machine:~/random
-> $ curl localhost:8000/profile?user_id=5432; echo
{"user_id": 5432, "given_name": "John", "surname": "Smith", "pic": "JSmith.png"}
[2019-08-17 19:07:02] 0 x10an14@machine:~/random
-> $ curl localhost:8000/profile2?user_id=5432; echo
{"user_id": 5432, "given_name": "John", "surname": "Smith", "pic": "http://localhost:8000/photo?user_id=5432"}
[2019-08-17 19:07:07] 0 x10an14@machine:~/random
-> $ curl localhost:8000/profile2?user_id=5; echo
This URL must be called with a 'user_id={user_id}' parameter with a valid user_id.
[2019-08-17 19:07:12] 0 x10an14@machine:~/random
-> $
Zeethulhu
@Zeethulhu
@x10an14 Thanks a bunch! I really appreciate you taking the time to help me out.
x10an14
@x10an14
You're welcome =)
Jackemuk
@jackemuk

@timothycrosley

I'm trying to validate a marshmallow field.UUID against uuid.uuid4.
It will validate that is not a valid uuid4 based on a regex, but when it's valid the error returned is:

{
    "errors": {
        "asset_id": "expected string or bytes-like object"
    }
}
from marshmallow import fields
from marshmallow.validate import Regexp
@hug.get('/asset/{asset_id}', output=hug.output_format.json)
`def return_data(body, request, response,
                 db: SqlalchemySession,
                 asset_id: fields.UUID(missing=uuid.uuid4(),                                                                                                             validate=Regexp(uuid4_check),
                                 allow_none=True)=None):

I'm using marshmallow=3.0.1, hug=2.5.6, python=3.6.8
I can't figure out what's happening.