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

docs: improve documentation #269

Merged
merged 4 commits into from
Aug 29, 2022
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
20 changes: 10 additions & 10 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

# Robyn

[![sansyrox](https://circleci.com/gh/sansyrox/robyn.svg?style=svg)](https://app.circleci.com/pipelines/github/sansyrox/robyn)
[![Twitter](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/robyn_oss)
[![Gitter](https://badges.gitter.im/robyn_/community.svg)](https://gitter.im/robyn_/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Downloads](https://static.pepy.tech/personalized-badge/robyn?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/robyn)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![GitHub tag](https://img.shields.io/github/tag/sansyrox/robyn?include_prereleases=&sort=semver&color=black)](https://github.com/sansyrox/robyn/releases/)
[![License](https://img.shields.io/badge/License-BSD_2.0-black)](#license)
[![Discord](https://img.shields.io/discord/999782964143603713?label=discord&logo=discord&logoColor=white&style=for-the-badge&color=blue)](https://discord.gg/qKF5sSnC)


[![view - Documentation](https://img.shields.io/badge/view-Documentation-blue?style=for-the-badge)](https://sansyrox.github.io/robyn/#/)

Robyn is an async Python backend server with a runtime written in Rust, btw.


Check out the talk at **PyCon Sweden 2021** about [Robyn: An async python web framework with a Rust runtime](https://www.youtube.com/watch?v=DK9teAs72Do)

## 📦 Installation

You can simply use Pip for installation.
Expand All @@ -30,9 +28,7 @@ pip install robyn
Or, with [conda-forge](https://conda-forge.org/)

```
conda config --add channels conda-forge
conda config --set channel_priority strict
conda install robyn
conda install -c conda-forge robyn
```

## 🤔 Usage
Expand All @@ -44,7 +40,7 @@ from robyn import Robyn
app = Robyn(__file__)

@app.get("/")
async def h(requests):
async def h(request):
return "Hello, world!"

app.start(port=5000)
Expand Down Expand Up @@ -108,16 +104,20 @@ To contribute to Robyn, make sure to first go through the [CONTRIBUTING.md](./CO

Thanks to all the contributors of the project. Robyn will not be what it is without all your support :heart:.

Special thanks to the [ PyO3 ](https://pyo3.rs/v0.13.2/) community and [ Andrew from PyO3-asyncio ](awestlake87/pyo3-asyncio) for their amazing libraries and their support for my queries. 💖
<a href="https://github.com/sansyrox/robyn/graphs/contributors">
<img src="https://contrib.rocks/image?repo=sansyrox/robyn" />
</a>


Special thanks to the [ PyO3 ](https://pyo3.rs/v0.13.2/) community and [ Andrew from PyO3-asyncio ](https://github.com/awestlake87/pyo3-asyncio) for their amazing libraries and their support for my queries. 💖

## ✨ Sponsors

These sponsors help us make the magic happen!

[![DigitalOcean Referral Badge](https://web-platforms.sfo2.cdn.digitaloceanspaces.com/WWW/Badge%201.svg)](https://www.digitalocean.com/?refcode=3f2b9fd4968d&utm_campaign=Referral_Invite&utm_medium=Referral_Program&utm_source=badge)

[![Shivay Lamba](https://avatars.githubusercontent.com/u/19529592?v=4 =100x100)](https://github.com/shivaylamba)

- [ Shivay Lamba ](https://github.com/shivaylamba)



9 changes: 7 additions & 2 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<!-- docs/_sidebar.md -->

- [Home](/)
- [Installation](installation.md)
- [Getting Started](getting-started.md)
- [Features](features.md)
- [API](api.md)
- [Examples](examples.md)
- [Architecture](architecture.md)
- [Comparison](comparison.md)
- [Plugins](plugins.md)
- [API](api.md)
- [Community Resources](community-resources.md)
- [Future Roadmap](roadmap.md)
- [Plugins](plugins.md)
- [Hosting](hosting.md)
- [Sponsors](sponsors.md)

291 changes: 3 additions & 288 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,290 +1,5 @@
# API Usage
## API

## Getting Started
Documentation of all the public facing APIs of Robyn.

```python

from robyn import Robyn

app = Robyn(__file__)

@app.get("/")
async def h():
return "Hello, world!"

app.start(port=5000, url="0.0.0.0") # url is optional, defaults to 127.0.0.1

```

Let us try to decipher the usage line by line.

> from robyn import Robyn

This statement just imports the Robyn structure from the robyn package.

> app = Robyn(__file__)

Here, we are creating the app object. We require the `__file__` object to mount the directory for hot reloading.

## Running the service

You can just use the command

```
python3 app.py
```

if you want to run the production version, and

```
python3 app.py --dev=true
```
if you want to enable hot reloading or the development version.

## Serving an HTTP Request


Robyn supports both sync methods and async methods for fetching requests. Every method gets a request object from the routing decorator.

The request object contains the `body` in PUT/POST/PATCH. The `header`s are available in every request object.

Robyn supports every HTTP request method. The examples of some of them are below:
### GET Request

```python3
@app.get("/")
async def h(request):
return "Hello World"
```

### POST Request

```python3
@app.post("/post")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
```

### PUT Request

```python3
@app.put("/put")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
```


### PATCH Request

```python3
@app.patch("/patch")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
```


### DELETE Request

```python3
@app.delete("/delete")
async def postreq(request):
return bytearray(request["body"]).decode("utf-8")
```


### Having Dynamic Routes
You can add params in the routes and access them from the request object.

```python3
@app.post("/jsonify/:id")
async def json(request):
print(request["params"]["id"])
return jsonify({"hello": "world"})
```

### Query Params

You can access query params from every HTTP method.

For the url: `http://localhost:5000/query?a=b`

You can use the following code snippet.

```python3
@app.get("/query")
async def query_get(request):
query_data = request["queries"]
return jsonify(query_data)
```

### Getting URL form data
You can access URL form data with the request object similar to how you access params.

```python3
@app.get("/get")
async def query(request):
form_data = request.get("queries", {})
print(form_data)
return jsonify({"queries": form_data})
```

### Returning a JSON Response
You can also serve JSON responses when serving HTTP request using the following way.

```python3
from robyn import jsonify

@app.post("/jsonify")
async def json(request):
print(request)
return jsonify({"hello": "world"})
```

### Serving a Directory
You can also use robyn to serve a directory. An example use case of this method would be when you want to build and serve a react/vue/angular/vanilla js app.

You would type the following command to generate the build directory.
```
yarn build
```

and you can then add the following method in your robyn app.

```python3
app.add_directory(route="/react_app_serving",directory_path="./build", index_file="index.html")

```


### Serving a Static File

If you don't want to serve static directory and want to just serve static_files, you can use the following method.
```python3
from robyn import Robyn, static_file

@app.get("/test")
async def test():
return static_file("index.html")

```

### Serving Headers
You can also add global headers for every request.

```python3
app.add_header("server", "robyn")

```

## Events

You can add startup and shutdown events in robyn. These events will execute before the requests have started serving and after the serving has been completed.

```python3

async def startup_handler():
print("Starting up")

app.startup_handler(startup_handler)

@app.shutdown_handler
def shutdown_handler():
print("Shutting down")
```

## WebSockets

You can now serve websockets using Robyn.

Firstly, you need to create a WebSocket Class and wrap it around your Robyn app.

```python3
from robyn import Robyn, static_file, jsonify, WS


app = Robyn(__file__)
websocket = WS(app, "/web_socket")
```

Now, you can define 3 methods for every web_socket for their life cycle, they are as follows:

```python3
@websocket.on("message")
def connect():
global i
i+=1
if i==0:
return "Whaaat??"
elif i==1:
return "Whooo??"
elif i==2:
return "*chika* *chika* Slim Shady."
elif i==3:
i= -1
return ""

@websocket.on("close")
def close():
return "Goodbye world, from ws"

@websocket.on("connect")
def message():
return "Hello world, from ws"

```

The three methods:
- "message" is called when the socket receives a message
- "close" is called when the socket is disconnected
- "connect" is called when the socket connects

To see a complete service in action, you can go to the folder [../integration_tests/base_routes.py](../integration_tests/base_routes.py)


### Usage

```python3
@websocket.on("message")
async def connect():
global i
i+=1
if i==0:
return "Whaaat??"
elif i==1:
return "Whooo??"
elif i==2:
return "*chika* *chika* Slim Shady."
elif i==3:
i= -1
return ""

@websocket.on("close")
async def close():
return "Goodbye world, from ws"

@websocket.on("connect")
async def message():
return "Hello world, from ws"

```

## Middlewares

You can use both sync and async functions for middlewares!

```python3
@app.before_request("/")
async def hello_before_request(request):
print(request)


@app.after_request("/")
def hello_after_request(request):
print(request)
```

## MultiCore Scaling

To run Robyn across multiple cores, you can use the following command:

`python3 app.py --workers=N --processes=N`
Coming Soon.....
Loading