File tree Expand file tree Collapse file tree 5 files changed +88
-0
lines changed Expand file tree Collapse file tree 5 files changed +88
-0
lines changed Original file line number Diff line number Diff line change @@ -44,3 +44,4 @@ to create it. There is some material not covered in the presentation as well.
44
44
1 . ` subprocess ` : illustrates executing a shell command from a Python script
45
45
using the ` subprocess ` module.
46
46
1 . ` xml-generator ` : code to generate a random XML documents.
47
+ 1 . ` fastapi ` : simple illustrations of using FastAPI for webservices.
Original file line number Diff line number Diff line change
1
+ # FastAPI
2
+
3
+ FastAPI is a nice framework to build web applications and handle http requests.
4
+
5
+ ## What is it?
6
+
7
+ * ` main.py ` : very simple hello world application.
8
+ * ` calculator.py ` : implementation of an API that allows to
9
+ * push numbers on a stack (push/<number >)
10
+ * perform a computation (compute/add or compute/mult)
11
+ * ` calculations.sh ` : shell script to illustrate use of the calculator API.
12
+
13
+ ## How to use it?
14
+
15
+ ### Hello world application
16
+ ``` bash
17
+ $ uvicorn main:app --reload
18
+ ```
19
+
20
+ ### Calculator
21
+ ``` bash
22
+ $ uvicorn calculatorapp --reload
23
+ $ curl --silent http://127.0.0.1:8000/push/5
24
+ $ curl --silent http://127.0.0.1:8000/push/7
25
+ $ curl --silent http://127.0.0.1:8000/compute/add
26
+ ```
Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env bash
2
+
3
+ # start the server with
4
+ # uvivorn calculator::app --reload
5
+ #
6
+
7
+ source ~ /.mamba_init.sh
8
+ mamba activate python_for_systems_programming
9
+
10
+ HOST=127.0.0.1:8000
11
+
12
+ echo " push 5"
13
+ curl --silent " http://$HOST /push/5"
14
+ echo " "
15
+
16
+ echo " push 7"
17
+ curl --silent " http://$HOST /push/7"
18
+ echo " "
19
+
20
+ echo " perform addition"
21
+ curl --silent " http://$HOST /compute/add" | json_pp
22
+ echo " "
Original file line number Diff line number Diff line change
1
+ from fastapi import FastAPI
2
+ from typing import Union
3
+
4
+ app = FastAPI ()
5
+ stack = []
6
+
7
+ @app .get ('/push/{value}' )
8
+ def push (value : float , q : Union [str , None ] = None ):
9
+ stack .append (value )
10
+
11
+ @app .get ('/compute/{operator}' )
12
+ def compute (operator : str , q : Union [str , None ] = None ):
13
+ right_operand = stack .pop ()
14
+ left_operand = stack .pop ()
15
+ if operator == 'add' :
16
+ result = left_operand + right_operand
17
+ elif operator == 'mult' :
18
+ result = left_operand * right_operand
19
+ return {'result' : result }
Original file line number Diff line number Diff line change
1
+ from typing import Union
2
+ from fastapi import FastAPI
3
+
4
+ app = FastAPI ()
5
+
6
+
7
+ @app .get ("/" )
8
+ def read_root ():
9
+ return {"Hello" : "World" }
10
+
11
+
12
+ @app .get ("/items/{item_id}" )
13
+ def read_item (item_id : int , q : Union [str , None ] = None ):
14
+ return {"item_id" : item_id , "q" : q }
15
+
16
+
17
+ @app .get ("/users/{user_id}&{category}" )
18
+ def read_item (user_id : int , category : str ,
19
+ q : Union [str , None ] = None ):
20
+ return {"user_id" : user_id , "category" : category , "q" : q }
You can’t perform that action at this time.
0 commit comments