Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mounting Mesop with other Python servers #803

Closed
wwwillchen opened this issue Aug 15, 2024 · 3 comments · Fixed by #892
Closed

Support mounting Mesop with other Python servers #803

wwwillchen opened this issue Aug 15, 2024 · 3 comments · Fixed by #892

Comments

@wwwillchen
Copy link
Collaborator

wwwillchen commented Aug 15, 2024

Motivation

This could satisfy a few advanced use cases:

Considerations

  • We'd like to eventually migrate from Flask to an async-friendly framework like Quart or Starlette so we shouldn't directly expose the framework so we have flexibility to do this migration. However, we should try to provide an API that's similar so the learning curve is reasonably small.

API proposal

Simple request

@me.route(methods=["GET", "POST"], path="/foo")
def route():
  return output

Async

@me.route(path="/books/{book_id}")
async def route(book_id: int):
  output = await call_api()
  return output

Path parameters

@me.route(methods=["GET", "POST"], path="/books/{book_id}")
def route(book_id: int):
  return output

Query params

@me.route(methods=["GET", "POST"], path="/books")
def route():
  qp = me.query_params()
  return do_work(qp["foo"])

output could be:

  • dict
  • json
  • binary file

Questions

@wwwillchen
Copy link
Collaborator Author

Perhaps we can just offer a way to mount apps, e.g.:

# app is any WSGI app
me.mount_app(app)

https://flask.palletsprojects.com/en/2.3.x/patterns/appdispatch/
https://github.com/django/asgiref/tree/main

@wwwillchen wwwillchen changed the title Support custom HTTP routes Support mounting Mesop with other Python servers Aug 16, 2024
@wwwillchen
Copy link
Collaborator Author

wwwillchen commented Aug 16, 2024

Turns out you can actually mount Mesop because me actually satisfies the WSGI protocol. Here's an example of combining a Mesop app and a FastAPI app together:
https://github.com/wwwillchen/mesop-fastapi

It works!

Next steps:

  • Expose me.wsgi_app which is just an alias of me
  • Create an actual guide - tell users about --timeout-graceful-shutdown for https://www.uvicorn.org/deployment/
  • Be able to configure Mesop dev mode as an environmental variable? (seems generally useful for debugging) - pretty simple - modify
    _app = create_app(prod_mode=True)
    .
  • JS file changes doesn't trigger uvicorn reload - fixable by following: https://www.uvicorn.org/settings/#reloading-with-watchfiles
  • Hot reloading doesn't work - this isn't a showstopper, but not nice. You can still run mesop foo.py on the part of the app that doesn't require the other Python server and get hot reloading.

@richard-to
Copy link
Collaborator

Nice, this sounds pretty awesome. This definitely could potentially solve quite a few issues, such as #564 and #472

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants