Skip to main content

GH3 Python WSGI nano framework.

Project description

gh3 Python WSGI nanoframework

if you are not already there, read this doc with API links working
in all their glory on the
[website](https://gh3-website.web.app/README.html)

First App

import gh3

def say_hello(ctx: gh3.Context):
  """A view handler that returns some text."""
  ctx.reply_text('hello, world')

# Create the app, add a route, and start the debug server.
app = gh3.App()
app.add_route('/', say_hello)
app.debug()

Here, we create an instance of gh3.App and we add a simple route to it, and start the debug server.

You can immediately see some features of the request handler:

  • You modify the response in place from the gh3.Context.
  • It is simple to make a simple textual response, using reply_text and there are analogs for reply_html and reply_json. These reply_ methods are shortcuts to setting the response data, the response content-type, and the response status code.

Installation

virtualenv -p python3.9 env
./env/bin/pip install gh3

Route handler arguments

Arguments from the route are available in ctx.endpoint_args. They are not passed to the handler functions as all handler functions take only the request context as an argument.

import gh3

def say_hello(ctx: gh3.Context):
  """A view handler that returns some text."""
  user = ctx.endpoint_args['user']
  ctx.reply_text(f'hello, {user}')

# Create the app, add a route, and start the debug server.
app = gh3.App()
app.add_route('/<user>', say_hello)
app.debug()

Routes, targets, and endpoints

As in the simple example above, there is a route '/' and a target, the say_hello function. What is not described there is that the endpoint is inferred from the function name, but can be set explicitly:

app.add_route('/', say_hello, endpoint='home')

The endpoint is used for reverse lookup of URLs.

More complex routing

The entire range of Werkzeug's routing is available by using add_rule and add_target. For example, to use Werkzeug's Submount rule factory, you should create the rule and add the target handlers manually.

rule = gh3.wz_routing.Submount(
    Rule('/', endpoint='blog/index'),
    Rule('/entry/<entry_slug>', endpoint='blog/show')
)

app.add_rule(rule)
app.add_target('blog/index', my_blog_index_handler)
app.add_target('blog/show', my_blog_show_handler)

Why?

I love Werkzeug, but I really don't love Flask (sorry, Armin). I swear I have used Flask a lot, in big production applications that serve billions of pages a year. There are patterns in Flask that have made it extremely hard for me to develop as part of a large (30+) engineering team, and these are the things intentionally left out of gh3. gh3 will never be as featured as Flask, and that's OK too. You'll forgive me these few 100 lines of well-tested code. And if you are still wondering why, please feel free to move on.

Project details


Download files

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

Source Distribution

gh3-1.0.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

gh3-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file gh3-1.0.1.tar.gz.

File metadata

  • Download URL: gh3-1.0.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for gh3-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7ae084822543565511ecd6cc3afcb3b6378d5055c22dc8e08b4d8cd43bd33c2a
MD5 fe7129ecb22dab5a98aa55e002e745f6
BLAKE2b-256 8075e40dd5220236924160da4f40b4c8cf537cf52a96107f315e6038eeb088c4

See more details on using hashes here.

File details

Details for the file gh3-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: gh3-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.26.0

File hashes

Hashes for gh3-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02f9698cfc49192daecb801443c6900b4a8ab43ee7a123ce5b5abde4435de92e
MD5 b395a057fa506e3dadf3a15f014c8e75
BLAKE2b-256 05a9cff9e84a07b731cbc6b649a40b5bce997f496747b87769ee70ec3448ab81

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page