示例#1
0
class CelsiusApp(App):
    """
    Webanwendung zum Konvertieren von Celisus-Grad in Fahrenheit-Grad.

    Diese sehr einfache Anwendung demonstriert die Verwendung des Server-Frameworks.
    Die Klasse Celsius-App benötigt zwei Methoden:
    1. Registrierung der Routen
    2. Definition eines Request-Handlers
    """
    def register_routes(self):
        self.add_route('',
                       self.celsius)  # there is only one route for everything

    def celsius(self, request, response, pathmatch=None):
        msg = ""
        if 'celsius' in request.params:  # check if parameter is given
            try:  # calculate
                fahrenheit = float(request.params['celsius']) * 9 / 5 + 32
                msg = "{}° Celsius sind {:4.2f}° Fahrenheit".format(
                    request.params['celsius'], fahrenheit)
            except (ValueError, TypeError):
                msg = "Bitte eine Zahl eingeben."
        response.send_template('templates/celsius/celsius.tmpl', {'msg': msg})


if __name__ == '__main__':
    s = Webserver()
    s.add_app(CelsiusApp(prefix='celsius'))
    s.serve()
    webbrowser.open_new_tab("http://localhost:8080/celsius")
示例#2
0
                'current_user': {
                    'id': 815,
                    'icon': '☘',
                    'name': 'Tobias Findeisen'
                }
            })

    @staticmethod
    def page3(request, response, pathmatch):
        """The third page."""
        response.send_template(
            'page3.tmpl', {
                'magic_numbers': [7, 13, 23, 27, 42],
                'current_user': {
                    'id': 815,
                    'icon': '☘',
                    'name': 'Tobias Findeisen'
                },
                'message': "<i>HTML-Content  &#x2603;</i>"
            })


if __name__ == '__main__':
    s = Webserver()
    s.set_templating("jinja2")
    s.set_templating_path("templates.jinja2/skel")
    s.add_app(SkelApp())
    s.add_app(StaticApp(prefix='static', path='static'))

    s.serve()