Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: deprecate views #1096

Merged
merged 8 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions docs_src/src/components/documentation/ApiDocs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ const guides = [
name: 'Websockets',
description: 'Learn how to use Websockets in Robyn.',
},
{
href: '/documentation/api_reference/views',
name: 'Code Organisation',
description: 'Learn about Views and SubRouters in Robyn.',
},

{
href: '/documentation/api_reference/exceptions',
name: 'Exceptions',
Expand All @@ -94,8 +88,6 @@ const guides = [
name: 'Multiprocess Execution',
description: 'Learn about the behaviour or variables during multithreading',
},


{
href: '/documentation/api_reference/using_rust_directly',
name: 'Direct Rust Usage',
Expand Down
27 changes: 11 additions & 16 deletions docs_src/src/components/documentation/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export const navigation = [
},
{ title: 'Templates', href: '/documentation/example_app/templates' },
{
title: 'SubRouters and Views',
href: '/documentation/example_app/subrouters_and_views',
title: 'SubRouters',
href: '/documentation/example_app/subrouters',
},
],
},
Expand Down Expand Up @@ -276,15 +276,6 @@ export const navigation = [
href: '/documentation/api_reference/websockets',
title: 'Websockets',
},
{
href: '/documentation/api_reference/views',
title: 'Code Organisation',
},
{
href: '/documentation/api_reference/dependency_injection',
title: 'Dependency Injection',
},

{
href: '/documentation/api_reference/exceptions',
title: 'Exceptions',
Expand All @@ -297,22 +288,26 @@ export const navigation = [
href: '/documentation/api_reference/advanced_features',
title: 'Advanced Features',
},
{
title: 'OpenAPI Documentation',
href: '/documentation/api_reference/openapi',
},
{
href: '/documentation/api_reference/multiprocess_execution',
title: 'Multiprocess Execution',
},
{
href: '/documentation/api_reference/using_rust_directly',
title: 'Using Rust Directly',
title: 'Direct Rust Usage',
},
{
href: '/documentation/api_reference/graphql-support',
title: 'GraphQL Support',
},
{
href: '/documentation/api_reference/openapi',
title: 'OpenAPI Documentation',
},
{
href: '/documentation/api_reference/dependency_injection',
title: 'Dependency Injection',
}
],
},
{
Expand Down
182 changes: 0 additions & 182 deletions docs_src/src/pages/documentation/api_reference/views.mdx

This file was deleted.

105 changes: 28 additions & 77 deletions docs_src/src/pages/documentation/example_app/subrouters_and_views.mdx
Original file line number Diff line number Diff line change
@@ -1,97 +1,48 @@
export const description =
'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.'

## Code Organization with SubRouters

## SubRouter and Views
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.

After implementing the application application, Batman wanted to split the codebase into multiple files.

This is when Robyn introduced him to the concept of routers and views.

### Routers

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.

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.

So the folder structure would look like this:

```bash
├── app.py
├── frontend.py
├── Dockerfile
└── requirements.txt
```

And the code would look like this:

```python {{ title: 'Creating a Router' }}
# frontend.py

from robyn.templating import JinjaTemplate
```python
from robyn import SubRouter
import os
import pathlib


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

@crime_router.get("/list")
def list_crimes():
return {"crimes": get_all_crimes()}

frontend = SubRouter(__name__, prefix="/frontend")
@crime_router.post("/report")
def report_crime(request):
crime_data = request.json()
return {"id": create_crime_report(crime_data)}

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

```python {{ title: 'Including a Router' }}
# app.py
@suspect_router.get("/list")
def list_suspects():
return {"suspects": get_all_suspects()}

from .frontend import frontend
@suspect_router.get("/:id")
def get_suspect(request, path_params):
suspect_id = path_params.id
return {"suspect": get_suspect_by_id(suspect_id)}


app.include_router(frontend)
# Include the subrouters in the main app
app.include_router(crime_router)
app.include_router(suspect_router)
```

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

### Views

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.

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.


The code would look like this:

<CodeGroup>
```python {{ title: 'Creating a decorator View' }}
from robyn import SyncView

@app.view("/sync/view/decorator")
def sync_decorator_view():
def get():
return "Hello, world!"

def post(request: Request):
body = request.body
return body
```


```python {{ title: 'Creating a View' }}
def sync_decorator_view():
def get():
return "Hello, world!"

def post(request: Request):
body = request.body
return body
- All crime-related routes are under `/crimes`
- All suspect-related routes are under `/suspects`

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



Expand Down
Loading
Loading