Skip to content

Commit

Permalink
Test Python server
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Aug 14, 2024
1 parent 1a1580e commit 8ebb8f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ Given you have a model class called `MyModel` in a package `mypackage` then the
BMI_MODULE=mypackage BMI_CLASS=MyModel run-bmi-server
```

For example [leakybucket](https://github.com/eWaterCycle/leakybucket-bmi):

```shell
pip install leakybucket
BMI_MODULE=leakybucket.leakybucket_bmi BMI_CLASS=LeakyBucketBmi run-bmi-server
```

### Julia provider

Given you have a model class called `MyModel` and a BMI called `BMI` inside the `MyPackage` package.
Expand Down
13 changes: 11 additions & 2 deletions python/remotebmi/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import contextlib
from os import environ
from traceback import format_exception
from typing import AsyncIterator

from bmipy import Bmi
from connexion import AsyncApp, ConnexionMiddleware
from connexion import AsyncApp, ConnexionMiddleware, problem
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
from connexion.resolver import RelativeResolver
from connexion.options import SwaggerUIOptions
import uvicorn

from remotebmi.server.build import from_env

def handle_model_error(request: ConnexionRequest, exc: Exception) -> ConnexionResponse:
exc_class = str(exc.__class__.__name__)
ext = {"traceback": format_exception(exc)}
return problem(500, type=exc_class, title="Model raised " + exc_class, detail=str(exc), ext=ext)

def make_app(model: Bmi):
@contextlib.asynccontextmanager
async def lifespan_handler(app: ConnexionMiddleware) -> AsyncIterator:
Expand All @@ -22,13 +30,14 @@ async def lifespan_handler(app: ConnexionMiddleware) -> AsyncIterator:
)

app.add_api("openapi.yaml", resolver=RelativeResolver("remotebmi.server.api"))
app.add_error_handler(Exception, handle_model_error)
return app

def main(**kwargs):
model = from_env()
app = make_app(model)
port = int(environ.get('BMI_PORT', 50051))
app.run(port=port, **kwargs)
uvicorn.run(app, port=port, **kwargs)

if __name__ == "__main__":
main()
7 changes: 4 additions & 3 deletions python/remotebmi/server/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import Literal
from bmipy import Bmi
from connexion import request
import numpy as np

from python.remotebmi.reserve import reserve_grid_edge_nodes, reserve_grid_face_, reserve_grid_nodes, reserve_grid_nodes_per_face, reserve_grid_padding, reserve_grid_shape, reserve_values, reserve_values_at_indices
from remotebmi.reserve import reserve_grid_edge_nodes, reserve_grid_face_, reserve_grid_nodes, reserve_grid_nodes_per_face, reserve_grid_padding, reserve_grid_shape, reserve_values, reserve_values_at_indices


def model() -> Bmi:
# get bmi model from context
return request.state.model


def initialize(config_file: str):
model().initialize(config_file)
def initialize(body: dict[Literal['config_file'], str]):
model().initialize(body['config_file'])


def update():
Expand Down

0 comments on commit 8ebb8f8

Please sign in to comment.