Skip to content

Conversation

@clju
Copy link

@clju clju commented May 29, 2025

This is extremely useful for cases where heavy information needs to be transmitted from server to client (and serializing it / putting it in the Mesop state would be prohibitely expensive and make the entire website very laggy).

Here is an example of it being used to create an endpoint serving video thumbnails:

@me.serve(rule='/thumbnail/<string:video_id>')
def serve_thumbnail(video_id: str):
  thumbnail_bytes = get_thumbnail_bytes_for_video(video_id)
  if not thumbnail_bytes:
    return f'No thumbnail found for {video_id}', 404, {}
  return Response(
      thumbnail_bytes,
      status=200,
      headers={
          'Content-Type': 'image/jpeg',
          'Cache-Control': (
              'public, max-age=600'
          ),
      },
  )

Copy link
Collaborator

@richard-to richard-to left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, it looks good. And I think it will be very helpful. Thanks for the pull request.

The only thing that slightly gives me pause is the parameterized url (i.e. the video_id part in your example '/thumbnail/string:video_id'). It's a very nice feature in that it makes the url look very clean and we get it from Flask.

The main drawback is just from consistency standpoint since me.page does not support that syntax yet, though it was discussed briefly.

I think that's a minor issue though and we can accept that inconsistency (maybe it will add more of a push to add similar support to me.page.


  • I think it would be good to add some playwright test so we have some integration test coverage for the feature
  • We should also update the docs
  • I tried to run the presubmit tests and there are some formatting errors.

I can try to help with adding the playwright tests and fixing some of the formatting errors because it is a bit of a pain to set up the Mesop dev env, especially internally.

This is extremely useful for cases where heavy information needs to be transmitted from server to client (and serializing it / putting it in the Mesop state would be prohibitely expensive and make the entire website very laggy).

Here is an example of it being used to create an endpoint serving video thumbnails:

@me.serve(rule='/thumbnail/<string:video_id>')
def serve_thumbnail(video_id: str):
  thumbnail_bytes = get_thumbnail_bytes_for_video(video_id)
  if not thumbnail_bytes:
    return f'No thumbnail found for {video_id}', 404, {}
  return Response(
      thumbnail_bytes,
      status=200,
      headers={
          'Content-Type': 'image/jpeg',
          'Cache-Control': (
              'public, max-age=600'
          ),
      },
  )
@clju
Copy link
Author

clju commented May 31, 2025

Oh thank you, I would definitely appreciate help with the playwright tests! I think I did manage to set-up the formatters though, normally they are passing now.

self._path_to_page_config[path] = page_config

def register_handler(
self, *, rule: str, handler: Callable[[], Tuple[str, int, Dict[str, str]]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] Where did you get this callable signature from - Callable[[], Tuple[str, int, Dict[str, str]]? Seems like it's a subset of what Flask expects in RouteCallable (tuple[ResponseValue, int, HeadersValue]?)

(https://github.com/pallets/flask/blob/a5f9742398c9429ef84ac8a57b0f3eb418394d9e/src/flask/typing.py#L84)

This does bring up the question of exposing Flask internals. Ideally we'd try to hide the Flask internals, though I think this will be somewhat hard to avoid. Maybe we can use TypeAliases to avoid explicitly in exposing the Flask types in the public serve function.

Also the script that does the type checking is not part of the local presubmits. You'll need to run it manually to check the types.

Usually something like this from the root of the directory (but assumes you've set up your dev env -- though you can also try to spin up a GitHub Codespaces)

./scripts/run_py_typecheck.sh

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I managed to run the typechecker. For the types, we could either copy Flasks's types and use the if TYPE_CHECKING trick, or... use "Any"? It looks like the type checker is ok with it for some reason?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ANY could be ok here so long as it is internal. For Mesop, we like to have annotations for the public API. Makes it more user friendly, so we need to be consistent with that.

We don't have to expose the Flask full type for Mesop. We could try just a subset of what Flask allows.

Note that we use "Any" to not have to rely on Flask's types.
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 this pull request may close these issues.

2 participants