Skip to main content

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.

Testing

We use Werkzeug's testing infrastructure, by calling gh3.App.tester to get a werkzeug test client and use it to make requests against your app. For many examples, check out the gh3 test suite.

You can test the request context in your views, e.g.

def text_view(ctx):
  assert '/' == ctx.req.path
  ctx.reply_text('ok')

app = gh3.App()
app.add_route('/', text_view)
resp = app.tester().get('/')
assert 200 == resp.status_code

We have made 2 assertions above:

  1. That the response is a 200
  2. That the request path at the view was /

You can use the pattern above to test anything in the context.

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.

Metadata

Release files for gh3 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gh3 1.0.4
File Size Uploaded
gh3-1.0.4.tar.gz 10.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gh3 1.0.4
File Interpreter ABI Platform
gh3-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / gh3-1.0.4.tar.gz

Download URL gh3-1.0.4.tar.gz
Size 10.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2919f543771d2c79aeadaad257a1fef9ef7cc9c0c8e72f78595177b0d7f22a71
BLAKE2b-256 checksum
How to use checksums
5a30bd9628177603cf4edc754403f0f0ceffc74867129881e8e34bf14103c961
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / gh3-1.0.4-py3-none-any.whl

Download URL gh3-1.0.4-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e2c19ca39524f12bed691c13882274629eac8b9965171395a826a0b9d3a097e
BLAKE2b-256 checksum
How to use checksums
2967b369656cb6625e8143d36f0e63a0fb46063456597b2c154fcb856b53b839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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