Skip to content

Commit 814f013

Browse files
authored
[website] Add function to save on WebAPIs (#493)
* add which function to keep in WebAPIs * add root function * update flaskapi and fastapi docs
1 parent 5fe135d commit 814f013

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

website/learn/code/deployment.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,55 @@ hal9 deploy webapp --type streamlit
4242

4343
Web APIs are applications that are designed for other computer programs or services to interoperate with, if you wanted to enable other web apps to use your previous app, you would do this as follows:
4444

45+
Always ensure any API's root path `@app.get("/")` function is kept, because Hal9 Web APIs use this endpoint to check the application's status.
46+
47+
Hal9 is able to run Python FastAPI or Flask apps.
48+
49+
### Fast API
4550
```python deploy
4651
from fastapi import FastAPI
4752
import random
4853

4954
app = FastAPI()
5055

5156
@app.get("/")
57+
def read_root():
58+
return {"success": True}
59+
60+
@app.get("/roll")
5261
async def roll():
5362
return f"You rolled a {random.randint(1, 6)}!"
5463
```
5564

65+
### Flask API
66+
```python deploy
67+
from flask import Flask, jsonify, request
68+
from asgiref.wsgi import WsgiToAsgi # WSGI compatible with Hal9 launcher
69+
import random
70+
71+
flaskapp = Flask(__name__)
72+
73+
@flaskapp.route("/", methods=['GET'])
74+
def read_root():
75+
return jsonify({"success": True})
76+
77+
@flaskapp.route("/roll", methods=['GET'])
78+
def roll():
79+
return f"You rolled a {random.randint(1, 6)}!"
80+
81+
app = WsgiToAsgi(flaskapp)
82+
```
83+
5684
When a chatbot generates an API, Hal9 automatically deploys and embeds the API endpoint; however, if you have to manually deploy an API you can accomplish this as follows:
5785

5886
```bash
5987
pip install hal9
6088

89+
# flask api app creation
6190
hal9 create webapi --type flask
6291
hal9 deploy webapi --type flask
92+
93+
# fast api app creation
94+
hal9 create webapi --type fastapi
95+
hal9 deploy webapi --type fastapi
6396
```

0 commit comments

Comments
 (0)