Skip to content

Commit 8aa97ea

Browse files
chore: deprecate views (#1096)
* chore: deprecate views * update docs * complete * fix issue * update * update * update * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7495342 commit 8aa97ea

File tree

12 files changed

+54
-442
lines changed

12 files changed

+54
-442
lines changed

docs_src/src/components/documentation/ApiDocs.jsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ const guides = [
6868
name: 'Websockets',
6969
description: 'Learn how to use Websockets in Robyn.',
7070
},
71-
{
72-
href: '/documentation/api_reference/views',
73-
name: 'Code Organisation',
74-
description: 'Learn about Views and SubRouters in Robyn.',
75-
},
76-
7771
{
7872
href: '/documentation/api_reference/exceptions',
7973
name: 'Exceptions',
@@ -94,8 +88,6 @@ const guides = [
9488
name: 'Multiprocess Execution',
9589
description: 'Learn about the behaviour or variables during multithreading',
9690
},
97-
98-
9991
{
10092
href: '/documentation/api_reference/using_rust_directly',
10193
name: 'Direct Rust Usage',

docs_src/src/components/documentation/Navigation.jsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ export const navigation = [
216216
},
217217
{ title: 'Templates', href: '/documentation/example_app/templates' },
218218
{
219-
title: 'SubRouters and Views',
220-
href: '/documentation/example_app/subrouters_and_views',
219+
title: 'SubRouters',
220+
href: '/documentation/example_app/subrouters',
221221
},
222222
],
223223
},
@@ -276,15 +276,6 @@ export const navigation = [
276276
href: '/documentation/api_reference/websockets',
277277
title: 'Websockets',
278278
},
279-
{
280-
href: '/documentation/api_reference/views',
281-
title: 'Code Organisation',
282-
},
283-
{
284-
href: '/documentation/api_reference/dependency_injection',
285-
title: 'Dependency Injection',
286-
},
287-
288279
{
289280
href: '/documentation/api_reference/exceptions',
290281
title: 'Exceptions',
@@ -297,22 +288,26 @@ export const navigation = [
297288
href: '/documentation/api_reference/advanced_features',
298289
title: 'Advanced Features',
299290
},
300-
{
301-
title: 'OpenAPI Documentation',
302-
href: '/documentation/api_reference/openapi',
303-
},
304291
{
305292
href: '/documentation/api_reference/multiprocess_execution',
306293
title: 'Multiprocess Execution',
307294
},
308295
{
309296
href: '/documentation/api_reference/using_rust_directly',
310-
title: 'Using Rust Directly',
297+
title: 'Direct Rust Usage',
311298
},
312299
{
313300
href: '/documentation/api_reference/graphql-support',
314301
title: 'GraphQL Support',
315302
},
303+
{
304+
href: '/documentation/api_reference/openapi',
305+
title: 'OpenAPI Documentation',
306+
},
307+
{
308+
href: '/documentation/api_reference/dependency_injection',
309+
title: 'Dependency Injection',
310+
}
316311
],
317312
},
318313
{

docs_src/src/pages/documentation/api_reference/views.mdx

Lines changed: 0 additions & 182 deletions
This file was deleted.

docs_src/src/pages/documentation/example_app/subrouters_and_views.mdx

Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,48 @@
11
export const description =
22
'Welcome to the Robyn API documentation. You will find comprehensive guides and documentation to help you start working with Robyn as quickly as possible, as well as support if you get stuck.'
33

4+
## Code Organization with SubRouters
45

5-
## SubRouter and Views
6+
As the application grew, Batman needed a way to organize his routes better. He decided to use Robyn's SubRouter feature to group related routes together.
67

7-
After implementing the application application, Batman wanted to split the codebase into multiple files.
8-
9-
This is when Robyn introduced him to the concept of routers and views.
10-
11-
### Routers
12-
13-
Routers are a way to split your application into multiple files. They allow you to group related endpoints together and make it easier to maintain your codebase.
14-
15-
For example, if you wanted to create a router for the frontend, you would create a file called `frontend.py`. This file would contain all the endpoints related to the frontend.
16-
17-
So the folder structure would look like this:
18-
19-
```bash
20-
├── app.py
21-
├── frontend.py
22-
├── Dockerfile
23-
└── requirements.txt
24-
```
25-
26-
And the code would look like this:
27-
28-
```python {{ title: 'Creating a Router' }}
29-
# frontend.py
30-
31-
from robyn.templating import JinjaTemplate
8+
```python
329
from robyn import SubRouter
33-
import os
34-
import pathlib
35-
3610

37-
current_file_path = pathlib.Path(__file__).parent.resolve()
38-
jinja_template = JinjaTemplate(os.path.join(current_file_path, "templates"))
11+
# Create a subrouter for crime-related routes
12+
crime_router = SubRouter(__file__, prefix="/crimes")
3913

14+
@crime_router.get("/list")
15+
def list_crimes():
16+
return {"crimes": get_all_crimes()}
4017

41-
frontend = SubRouter(__name__, prefix="/frontend")
18+
@crime_router.post("/report")
19+
def report_crime(request):
20+
crime_data = request.json()
21+
return {"id": create_crime_report(crime_data)}
4222

43-
@frontend.get("/")
44-
async def get_frontend(request):
45-
context = {"framework": "Robyn", "templating_engine": "Jinja2"}
46-
return jinja_template.render_template("index.html", **context)
47-
```
23+
# Create a subrouter for suspect-related routes
24+
suspect_router = SubRouter(__file__, prefix="/suspects")
4825

49-
```python {{ title: 'Including a Router' }}
50-
# app.py
26+
@suspect_router.get("/list")
27+
def list_suspects():
28+
return {"suspects": get_all_suspects()}
5129

52-
from .frontend import frontend
30+
@suspect_router.get("/:id")
31+
def get_suspect(request, path_params):
32+
suspect_id = path_params.id
33+
return {"suspect": get_suspect_by_id(suspect_id)}
5334

54-
55-
app.include_router(frontend)
35+
# Include the subrouters in the main app
36+
app.include_router(crime_router)
37+
app.include_router(suspect_router)
5638
```
5739

40+
SubRouters help organize related routes under a common prefix, making the code more maintainable and easier to understand. In this example:
5841

59-
### Views
60-
61-
Views are a way to split your application into multiple files. They allow you to group related endpoints together and make it easier to maintain your codebase.
62-
63-
For example, if you wanted to create a view for the frontend, you would create a file called `frontend.py`. This file would contain all the endpoints related to the frontend.
64-
65-
66-
The code would look like this:
67-
68-
<CodeGroup>
69-
```python {{ title: 'Creating a decorator View' }}
70-
from robyn import SyncView
71-
72-
@app.view("/sync/view/decorator")
73-
def sync_decorator_view():
74-
def get():
75-
return "Hello, world!"
76-
77-
def post(request: Request):
78-
body = request.body
79-
return body
80-
```
81-
82-
83-
```python {{ title: 'Creating a View' }}
84-
def sync_decorator_view():
85-
def get():
86-
return "Hello, world!"
87-
88-
def post(request: Request):
89-
body = request.body
90-
return body
42+
- All crime-related routes are under `/crimes`
43+
- All suspect-related routes are under `/suspects`
9144

92-
app.add_view("/sync/view/decorator", sync_decorator_view)
93-
```
94-
</CodeGroup>
45+
This organization makes it clear which routes handle what functionality and keeps related code together.
9546

9647

9748

0 commit comments

Comments
 (0)