abhijitkumarjha88192 commited on
Commit
662a446
1 Parent(s): 38d3ab9
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. app.py +65 -65
  3. main.py +74 -5
Dockerfile CHANGED
@@ -8,4 +8,4 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  COPY . .
10
 
11
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
8
 
9
  COPY . .
10
 
11
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,86 +1,86 @@
1
- from typing import Optional
2
- from fastapi import FastAPI, APIRouter, Query
3
- from testpkg.finit import api_router, app
4
- from testpkg.schemas import MyDataModel, MyDataSearchResults, CreateMyDataModelEntity
5
- from testpkg.MyDataList import myDataList
6
 
7
 
8
- @api_router.get("/datas/getall", status_code=200)
9
- def get_all() -> dict:
10
- return {"data": myDataList}
11
 
12
- # @api_router.get("/add_rows", status_code=200)
13
- # def add_rows(rowstr:str) -> dict:
14
- # return {"data": arr}
15
 
16
 
17
- # Updated using to use a response_model
18
- # https://fastapi.tiangolo.com/tutorial/response-model/
19
- @api_router.get("/datas/get/{data_id}", status_code=200)
20
- def get_data(*, data_id: int) -> dict:
21
- """
22
- Fetch a single data by ID
23
- """
24
 
25
- result = [x for x in myDataList if x["id"] == data_id]
26
- if result:
27
- return result[0]
28
 
29
 
30
- # Updated using the FastAPI parameter validation `Query` class
31
- # # https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
32
- @api_router.get("/datas/search", status_code=200)
33
- def search_data(*, keyword: Optional[str] = Query(None,min_length=3), max_results: Optional[int] = 3) -> dict:
34
- """
35
- Search for datas based on text keyword
36
- """
37
- if not keyword:
38
- # we use Python list slicing to limit results
39
- # based on the max_results query parameter
40
- return {"results": myDataList[:max_results]}
41
 
42
- results = filter(lambda x: keyword.lower() in x["text"].lower(), myDataList)
43
- return {"results": list(results)[:max_results]}
44
 
45
 
46
- # New addition, using Pydantic model `RecipeCreate` to define
47
- # the POST request body
48
- @api_router.post("/datas/create", status_code=201, response_model=MyDataModel)
49
- def create_data(*, my_data: CreateMyDataModelEntity) -> MyDataModel:
50
- """
51
- Create a new data (in memory only)
52
- """
53
- new_entry_id = len(myDataList) + 1
54
- entry = MyDataModel(
55
- id=new_entry_id,
56
- text=my_data.text,
57
- description=my_data.description
58
- )
59
- myDataList.append(entry.model_dump())
60
 
61
- return entry
62
 
63
 
64
- # Updated using to use a response_model
65
- # https://fastapi.tiangolo.com/tutorial/response-model/
66
- @api_router.get("/datas/delete/{data_id}", status_code=200)
67
- def delete_data(*, data_id: int) -> dict:
68
- """
69
- Fetch a single data by ID
70
- """
71
 
72
- result = [x for x in myDataList if x["id"] == data_id]
73
- if result:
74
- myDataList.remove(result[0])
75
 
76
- return {"datas":myDataList}
77
 
78
 
79
 
80
- if __name__ == "__main__":
81
- # Use this for debugging purposes only
82
- import uvicorn
83
 
84
- app.include_router(api_router)
85
 
86
- uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug")
 
1
+ # from typing import Optional
2
+ # from fastapi import FastAPI, APIRouter, Query
3
+ # from testpkg.finit import api_router, app
4
+ # from testpkg.schemas import MyDataModel, MyDataSearchResults, CreateMyDataModelEntity
5
+ # from testpkg.MyDataList import myDataList
6
 
7
 
8
+ # @api_router.get("/datas/getall", status_code=200)
9
+ # def get_all() -> dict:
10
+ # return {"data": myDataList}
11
 
12
+ # # @api_router.get("/add_rows", status_code=200)
13
+ # # def add_rows(rowstr:str) -> dict:
14
+ # # return {"data": arr}
15
 
16
 
17
+ # # Updated using to use a response_model
18
+ # # https://fastapi.tiangolo.com/tutorial/response-model/
19
+ # @api_router.get("/datas/get/{data_id}", status_code=200)
20
+ # def get_data(*, data_id: int) -> dict:
21
+ # """
22
+ # Fetch a single data by ID
23
+ # """
24
 
25
+ # result = [x for x in myDataList if x["id"] == data_id]
26
+ # if result:
27
+ # return result[0]
28
 
29
 
30
+ # # Updated using the FastAPI parameter validation `Query` class
31
+ # # # https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
32
+ # @api_router.get("/datas/search", status_code=200)
33
+ # def search_data(*, keyword: Optional[str] = Query(None,min_length=3), max_results: Optional[int] = 3) -> dict:
34
+ # """
35
+ # Search for datas based on text keyword
36
+ # """
37
+ # if not keyword:
38
+ # # we use Python list slicing to limit results
39
+ # # based on the max_results query parameter
40
+ # return {"results": myDataList[:max_results]}
41
 
42
+ # results = filter(lambda x: keyword.lower() in x["text"].lower(), myDataList)
43
+ # return {"results": list(results)[:max_results]}
44
 
45
 
46
+ # # New addition, using Pydantic model `RecipeCreate` to define
47
+ # # the POST request body
48
+ # @api_router.post("/datas/create", status_code=201, response_model=MyDataModel)
49
+ # def create_data(*, my_data: CreateMyDataModelEntity) -> MyDataModel:
50
+ # """
51
+ # Create a new data (in memory only)
52
+ # """
53
+ # new_entry_id = len(myDataList) + 1
54
+ # entry = MyDataModel(
55
+ # id=new_entry_id,
56
+ # text=my_data.text,
57
+ # description=my_data.description
58
+ # )
59
+ # myDataList.append(entry.model_dump())
60
 
61
+ # return entry
62
 
63
 
64
+ # # Updated using to use a response_model
65
+ # # https://fastapi.tiangolo.com/tutorial/response-model/
66
+ # @api_router.get("/datas/delete/{data_id}", status_code=200)
67
+ # def delete_data(*, data_id: int) -> dict:
68
+ # """
69
+ # Fetch a single data by ID
70
+ # """
71
 
72
+ # result = [x for x in myDataList if x["id"] == data_id]
73
+ # if result:
74
+ # myDataList.remove(result[0])
75
 
76
+ # return {"datas":myDataList}
77
 
78
 
79
 
80
+ # if __name__ == "__main__":
81
+ # # Use this for debugging purposes only
82
+ # import uvicorn
83
 
84
+ # app.include_router(api_router)
85
 
86
+ # uvicorn.run(app, host="0.0.0.0", port=7860, log_level="debug")
main.py CHANGED
@@ -1,7 +1,76 @@
1
- from fastapi import FastAPI
 
 
 
 
2
 
3
- app = FastAPI()
4
 
5
- @app.get("/")
6
- def read_root():
7
- return {"Hello": "World! this is webui fastapi"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+ from fastapi import FastAPI, APIRouter, Query
3
+ from testpkg.finit import api_router, app
4
+ from testpkg.schemas import MyDataModel, MyDataSearchResults, CreateMyDataModelEntity
5
+ from testpkg.MyDataList import myDataList
6
 
 
7
 
8
+ @api_router.get("/datas/getall", status_code=200)
9
+ def get_all() -> dict:
10
+ return {"data": myDataList}
11
+
12
+ # @api_router.get("/add_rows", status_code=200)
13
+ # def add_rows(rowstr:str) -> dict:
14
+ # return {"data": arr}
15
+
16
+
17
+ # Updated using to use a response_model
18
+ # https://fastapi.tiangolo.com/tutorial/response-model/
19
+ @api_router.get("/datas/get/{data_id}", status_code=200)
20
+ def get_data(*, data_id: int) -> dict:
21
+ """
22
+ Fetch a single data by ID
23
+ """
24
+
25
+ result = [x for x in myDataList if x["id"] == data_id]
26
+ if result:
27
+ return result[0]
28
+
29
+
30
+ # Updated using the FastAPI parameter validation `Query` class
31
+ # # https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
32
+ @api_router.get("/datas/search", status_code=200)
33
+ def search_data(*, keyword: Optional[str] = Query(None,min_length=3), max_results: Optional[int] = 3) -> dict:
34
+ """
35
+ Search for datas based on text keyword
36
+ """
37
+ if not keyword:
38
+ # we use Python list slicing to limit results
39
+ # based on the max_results query parameter
40
+ return {"results": myDataList[:max_results]}
41
+
42
+ results = filter(lambda x: keyword.lower() in x["text"].lower(), myDataList)
43
+ return {"results": list(results)[:max_results]}
44
+
45
+
46
+ # New addition, using Pydantic model `RecipeCreate` to define
47
+ # the POST request body
48
+ @api_router.post("/datas/create", status_code=201, response_model=MyDataModel)
49
+ def create_data(*, my_data: CreateMyDataModelEntity) -> MyDataModel:
50
+ """
51
+ Create a new data (in memory only)
52
+ """
53
+ new_entry_id = len(myDataList) + 1
54
+ entry = MyDataModel(
55
+ id=new_entry_id,
56
+ text=my_data.text,
57
+ description=my_data.description
58
+ )
59
+ myDataList.append(entry.model_dump())
60
+
61
+ return entry
62
+
63
+
64
+ # Updated using to use a response_model
65
+ # https://fastapi.tiangolo.com/tutorial/response-model/
66
+ @api_router.get("/datas/delete/{data_id}", status_code=200)
67
+ def delete_data(*, data_id: int) -> dict:
68
+ """
69
+ Fetch a single data by ID
70
+ """
71
+
72
+ result = [x for x in myDataList if x["id"] == data_id]
73
+ if result:
74
+ myDataList.remove(result[0])
75
+
76
+ return {"datas":myDataList}