Skip to content

Commit

Permalink
Add example of using @web_endpoint with lifecycle class (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
thundergolfer authored Oct 20, 2023
1 parent a622862 commit 9e8cf52
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 07_web_endpoints/basic_web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import modal
from modal import web_endpoint

stub = modal.Stub(name="example-lifecycle-web")

# Hello world!
#
# This is as simple as it gets. A GET endpoint which
# returns a string.


@stub.function()
@web_endpoint()
def hello():
return "Hello world!"


# Lifecycle-based.
#
# Web endpoints can be methods on a [lifecycle class](/docs/guide/lifecycle-functions#container-lifecycle-functions-and-parameters).
# This example will only set the `val` instance variable once, on container startup.
# But note that they don't need the [`modal.method`](/docs/reference/modal.method#modalmethod) decorator.


@stub.cls()
class WebApp:
def __enter__(self):
print("🏁 Startup up!")
self.val = "Hello world"

@web_endpoint()
def web(self):
return {"message": self.val}

0 comments on commit 9e8cf52

Please sign in to comment.