Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Responder

 Responder

Responder – https://python-responder.org

Talk given at:

Django Friends – Vienna 20.11.2018.
https://www.meetup.com/Django-Vienna/events/246063480/

Dražen Lučanin

November 20, 2018
Tweet

More Decks by Dražen Lučanin

Other Decks in Programming

Transcript

  1. Responder
    Dražen Lučanin

    @metakermit

    Django Friends – Vienna 20.11.2018.

    View Slide

  2. Hello, world!
    import responder
    api = responder.API()
    @api.route("/{greeting}")
    async def greet_world(req, resp, *, greeting):
    resp.text = f"{greeting}, world!"
    if __name__ == '__main__':
    api.run()

    View Slide

  3. Another Python web
    framework in 2018?
    • “A familiar HTTP Service Framework”

    • By Kenneth Reitz (Requests)

    • Kind of like an async Python web framework for humans

    • Not reinventing the wheel, but combining best practices

    • “Take Falcon, make it more like Flask & then give it some
    upgrades” – Kenneth Reitz @ Import This, Ep. 17

    View Slide

  4. • ASGI – async / await

    • f-string syntax routes

    • Built-in static file server

    • Single-page webapp support

    • Built-in Requests for tests

    • Meta-framework – mount any ASGI / WSGI app at a subroute

    • GraphQL support
    … some upgrades?

    View Slide

  5. Mostly built in one month
    • How?

    • #hacktoberfest2018

    • https://www.starlette.io/ by Tom Christie

    • Starlette is to Responder what Werkzeug is to Flask

    • So let’s go over Responder’s features…

    View Slide

  6. ASGI – async / await
    • Starlette – an ASGI implemented on top of uvicorn

    • async background tasks without Celery – no blocking
    import time
    @api.route("/incoming")
    async def receive_incoming(req, resp):
    @api.background.task
    def process_data(data):
    """Just sleeps for three seconds, as a demo."""
    time.sleep(3)
    # Parse the incoming data as form-encoded.
    # Note: 'json' and 'yaml' formats are also automatically supported.
    data = await req.media()
    # Process the data (in the background).
    process_data(data)
    # Immediately respond that upload was successful.
    resp.media = {'success': True}

    View Slide

  7. f-string syntax routes
    • The beloved Python 3 feature in action
    @api.route("/hello/{who}")
    def hello_to(req, resp, *, who):
    resp.text = f"hello, {who}!"

    View Slide

  8. Built-in static file server
    • Files from the static/ folder served using WhiteNoise

    • Single-page webapp support – all routes passed to index.html
    api.add_route("/", static=True)

    View Slide

  9. “What about an ORM?”
    – every Django developer
    • There are some solutions, but it’s still early days

    • GINO – https://github.com/fantix/gino

    • a lightweight asynchronous ORM built on top
    of SQLAlchemy core for Python asyncio

    • Peewee-async – https://peewee-async.readthedocs.io/

    • Responder comes with Marshmallow – https://
    marshmallow.readthedocs.io/ (a serialisation standard)

    View Slide

  10. Built-in Requests for tests
    import myapi
    @pytest.fixture
    def api():
    return myapi.api
    def test_response(api):
    hello = "hello, world!"
    @api.route('/some-url')
    def some_view(req, resp):
    resp.text = hello
    r = api.requests.get(url=api.url_for(some_view))
    assert r.text == hello

    View Slide

  11. How to start?
    • https://github.com/metakermit/responder-react

    • A simple Responder backend & React frontend starter kit

    • Using https://parceljs.org/ – a fast, 0-config web app bundler
    pip install -U cookiecutter
    cookiecutter gh:metakermit/responder-react
    cd helloresponderreact
    # local environment
    bin/install
    bin/start
    # docker environment
    docker-compose up

    View Slide

  12. Thanks!

    View Slide