diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b7b823bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +__pycache__ +.ipynb_checkpoints +*.egg-info + + +# Editor stuff +*.swp +.idea +.vscode diff --git a/README.md b/README.md index 2c7cd501..63b6856c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,17 @@ ## Development guide -### Building the docker image for local execution +### Running the COVID calculator app locally + +``` +pip install -e . # At the root of the repository +python -m cara.apps.calculator +``` + +Then visit http://localhost:8080/calculator. + + +### Building the whole environment for local execution **Simulate the docker build that takes place on openshift with:** diff --git a/cara/apps/calculator/__init__.py b/cara/apps/calculator/__init__.py index e69de29b..e099853b 100644 --- a/cara/apps/calculator/__init__.py +++ b/cara/apps/calculator/__init__.py @@ -0,0 +1,55 @@ +import json +from pathlib import Path + +from tornado.web import Application, RequestHandler, StaticFileHandler + +import cara.models + + +def build_model(request: dict) -> cara.models.Model: + return None + + +def build_response(model: cara.models.Model): + return {'items': 'foobar'} + + +class ConcentrationModel(RequestHandler): + def post(self): + requested_model_config = { + name: self.get_argument(name) for name in self.request.arguments + } + try: + model = build_model(requested_model_config) + except (KeyboardInterrupt, SystemExit): + raise + except Exception as err: + response_json = {'code': 400, 'error': f'Your request was invalid {err}'} + self.set_status(400) + self.finish(json.dumps(response_json)) + return + + response_json = build_response(model) + response_json['room_name'] = requested_model_config.get('room_name', 'unknown') + self.write(response_json) + + +def make_app(debug=False, prefix='/calculator'): + static_dir = Path(__file__).absolute().parent / 'static' + urls = [ + ( + prefix + r'()', StaticFileHandler, {'path': static_dir / 'form.html'} + ), + ( + prefix + r'/api/calculator', ConcentrationModel + ), + ( + prefix + r'/static/(.*)', + StaticFileHandler, + {'path': static_dir} + ), + ] + return Application( + urls, + debug=debug, + ) diff --git a/cara/apps/calculator/__main__.py b/cara/apps/calculator/__main__.py new file mode 100644 index 00000000..0db54a65 --- /dev/null +++ b/cara/apps/calculator/__main__.py @@ -0,0 +1,27 @@ +import argparse + +from tornado.ioloop import IOLoop +from tornado.options import define, options + +from . import make_app + + +def configure_parser(parser): + parser.add_argument( + "--no-debug", help="Don't enable debug mode", + action="store_false", + ) + return parser + + +def main(): + parser = argparse.ArgumentParser() + args = configure_parser(parser) + args = parser.parse_args() + app = make_app(debug=args.no_debug) + app.listen(8080) + IOLoop.instance().start() + + +if __name__ == '__main__': + main() diff --git a/cara/apps/calculator/static/form.html b/cara/apps/calculator/static/form.html index e69de29b..e696ec65 100644 --- a/cara/apps/calculator/static/form.html +++ b/cara/apps/calculator/static/form.html @@ -0,0 +1,86 @@ + + + + + + +
+ + + +
+ + +
+ The results will go here: +
+ + + + + \ No newline at end of file