-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of using @web_endpoint with lifecycle class (#472)
- Loading branch information
1 parent
a622862
commit 9e8cf52
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |