twisted / twisted/klein

Add a doc page for application composition

Open
#141 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
838
Forks
123
Avg merge
7h 58m
Merged PRs (30d)
12

Description

As noted, in #73, we can already structure klein apps into parts that can be composed together.

Let's write add an example to the docs, since it's not super obvious how to do that.

I tweaked @glyph's example code into something I think may work:

from klein import route, run, Klein

class HelloApplication(object):
    app = Klein()

    @app.route("/")
    def hello(self, request):
        return "Hello!"


class MathApplication(object):
    app = Klein()

    @staticmethod
    def numberify(string):
        if "." in string:
            return float(string)
        else:
            return int(string)

    @app.route("/")
    def root(self, request):
        return "Math happens here."

    @app.route("/add/<a>/<b>")
    def add(self, request, a, b):
        return "{}".format(self.numberify(a) + self.numberify(b))

    @app.route("/subtract/<a>/<b>")
    def subtract(self, request, a, b):
        return "{}".format(self.numberify(a) - self.numberify(b))

    @app.route("/multiply/<a>/<b>")
    def subtract(self, request, a, b):
        return "{}".format(self.numberify(a) * self.numberify(b))

    @app.route("/divide/<a>/<b>")
    def subtract(self, request, a, b):
        return "{}".format(self.numberify(a) / self.numberify(b))

    @app.handle_errors(ValueError)
    def valueError(self, request, failure):
        return "Invalid inputs provided."


class Application(object):
    app = Klein()

    @app.route("/")
    def root(self, request):
        return "This is a web application composed from multiple applications."

    @app.route("/hello/", branch=True)
    def hello(self, request):
        return HelloApplication().app.resource()

    @app.route("/math/", branch=True)
    def math(self, request):
        return MathApplication().app.resource()


if __name__ == '__main__':
    application = Application()
    application.app.run("localhost", 8080)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing documentation structure and the application-composition example provided in this issue. Add a documentation page explaining how Klein applications can be composed, using a corrected, runnable version of the example. Done means a newcomer can find and follow the example to understand composition.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.