Skip to content

Commit

Permalink
chore: deprecate views (sparckles#1096)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
sansyrox and pre-commit-ci[bot] authored Dec 30, 2024
1 parent 7495342 commit 8aa97ea
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 442 deletions.
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

0 comments on commit 8aa97ea

Please sign in to comment.