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

Update docs with changes done in 20.3 #1822

Merged
merged 2 commits into from
Mar 31, 2020
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
23 changes: 4 additions & 19 deletions docs/sanic/class_based_views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ using all these methods would look like the following.
from sanic.views import HTTPMethodView
from sanic.response import text

app = Sanic('some_name')
app = Sanic("class_views_example")

class SimpleView(HTTPMethodView):

def get(self, request):
return text('I am get method')

def post(self, request):
# You can also use async syntax
async def post(self, request):
return text('I am post method')

def put(self, request):
Expand All @@ -49,22 +50,6 @@ using all these methods would look like the following.

app.add_route(SimpleView.as_view(), '/')

You can also use `async` syntax.

.. code-block:: python

from sanic import Sanic
from sanic.views import HTTPMethodView
from sanic.response import text

app = Sanic('some_name')

class SimpleAsyncView(HTTPMethodView):

async def get(self, request):
return text('I am async get method')

app.add_route(SimpleAsyncView.as_view(), '/')

URL parameters
--------------
Expand Down Expand Up @@ -154,7 +139,7 @@ lambda:
from sanic.views import CompositionView
from sanic.response import text

app = Sanic(__name__)
app = Sanic("composition_example")

def get_handler(request):
return text('I am a get method')
Expand Down
4 changes: 2 additions & 2 deletions docs/sanic/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Any variables defined with the `SANIC_` prefix will be applied to the sanic conf

.. code-block:: python

app = Sanic(load_env='MYAPP_')
app = Sanic(__name__, load_env='MYAPP_')

Then the above variable would be `MYAPP_REQUEST_TIMEOUT`. If you want to disable loading from environment variables you can set it to `False` instead:

.. code-block:: python

app = Sanic(load_env=False)
app = Sanic(__name__, load_env=False)

From an Object
~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions docs/sanic/debug_mode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and the Automatic Reloader will be activated.
from sanic import Sanic
from sanic.response import json

app = Sanic()
app = Sanic(__name__)

@app.route('/')
async def hello_world(request):
Expand All @@ -43,7 +43,7 @@ the ``auto_reload`` argument will activate or deactivate the Automatic Reloader.
from sanic import Sanic
from sanic.response import json

app = Sanic()
app = Sanic(__name__)

@app.route('/')
async def hello_world(request):
Expand Down
4 changes: 2 additions & 2 deletions docs/sanic/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ You can also add an exception handler as such:
async def server_error_handler(request, exception):
return text("Oops, server error", status=500)

app = Sanic()
app = Sanic("error_handler_example")
app.error_handler.add(Exception, server_error_handler)

In some cases, you might want to add some more error handling
Expand All @@ -77,7 +77,7 @@ can subclass Sanic's default error handler as such:
# You custom error handling logic...
return super().default(request, exception)

app = Sanic()
app = Sanic("custom_error_handler_example")
app.error_handler = CustomErrorHandler()

Useful exceptions
Expand Down
2 changes: 1 addition & 1 deletion docs/sanic/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can also install Sanic from `conda-forge <https://anaconda.org/conda-forge/s
from sanic import Sanic
from sanic.response import json

app = Sanic()
app = Sanic("hello_example")

@app.route("/")
async def test(request):
Expand Down
2 changes: 1 addition & 1 deletion docs/sanic/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Sanic aspires to be simple
from sanic import Sanic
from sanic.response import json

app = Sanic()
app = Sanic("App Name")

@app.route("/")
async def test(request):
Expand Down
6 changes: 3 additions & 3 deletions docs/sanic/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A simple example using default settings would be like this:
from sanic.log import logger
from sanic.response import text

app = Sanic('test')
app = Sanic('logging_example')

@app.route('/')
async def test(request):
Expand Down Expand Up @@ -47,7 +47,7 @@ initialize ``Sanic`` app:

.. code:: python

app = Sanic('test', log_config=LOGGING_CONFIG)
app = Sanic('logging_example', log_config=LOGGING_CONFIG)

And to close logging, simply assign access_log=False:

Expand Down Expand Up @@ -100,4 +100,4 @@ Log Context Parameter Parameter Value Datatype

The default access log format is ``%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)d %(byte)d``

.. _python3 logging API: https://docs.python.org/3/howto/logging.html
.. _python3 logging API: https://docs.python.org/3/howto/logging.html
2 changes: 1 addition & 1 deletion docs/sanic/middleware.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ the one you instantiate your app in.

.. code-block:: python

app = Sanic()
app = Sanic(__name__)

async def setup_db(app, loop):
app.db = await db_setup()
Expand Down
2 changes: 1 addition & 1 deletion docs/sanic/nginx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ and in Nginx config.
from sanic import Sanic
from sanic.response import text

app = Sanic("Sanic Example")
app = Sanic("proxied_example")
app.config.FORWARDED_SECRET = "YOUR SECRET"

@app.get("/")
Expand Down
12 changes: 4 additions & 8 deletions docs/sanic/request_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ The difference between Request.args and Request.query_args for the queryset `?ke
"url": request.url,
"query_string": request.query_string,
"args": request.args,
"raw_args": request.raw_args,
"query_args": request.query_args,
})

Expand All @@ -72,12 +71,9 @@ The difference between Request.args and Request.query_args for the queryset `?ke
"url":"http:\/\/0.0.0.0:8000\/test_request_args?key1=value1&key2=value2&key1=value3",
"query_string":"key1=value1&key2=value2&key1=value3",
"args":{"key1":["value1","value3"],"key2":["value2"]},
"raw_args":{"key1":"value1","key2":"value2"},
"query_args":[["key1","value1"],["key2","value2"],["key1","value3"]]
}

- `raw_args` contains only the first entry of `key1`. Will be deprecated in the future versions.

- `files` (dictionary of `File` objects) - List of files that have a name, body, and type

.. code-block:: python
Expand Down Expand Up @@ -206,7 +202,7 @@ The output will be:
Accessing values using `get` and `getlist`
------------------------------------------

The `request.args` returns a subclass of `dict` called `RequestParameters`.
The `request.args` returns a subclass of `dict` called `RequestParameters`.
The key difference when using this object is the distinction between the `get` and `getlist` methods.

- `get(key, default=None)` operates as normal, except that when the value of
Expand All @@ -228,14 +224,14 @@ The key difference when using this object is the distinction between the `get` a
from sanic import Sanic
from sanic.response import json

app = Sanic(name="example")
app = Sanic(__name__)

@app.route("/")
def get_handler(request):
return json({
"p1": request.args.getlist("p1")
})

Accessing the handler name with the request.endpoint attribute
--------------------------------------------------------------

Expand All @@ -247,7 +243,7 @@ route will return "hello".
from sanic.response import text
from sanic import Sanic

app = Sanic()
app = Sanic(__name__)

@app.get("/")
def hello(request):
Expand Down
4 changes: 2 additions & 2 deletions docs/sanic/sockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ IPv6 example:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.bind(('::', 7777))

app = Sanic()
app = Sanic("ipv6_example")


@app.route("/")
Expand Down Expand Up @@ -46,7 +46,7 @@ UNIX socket example:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(server_socket)

app = Sanic()
app = Sanic("unix_socket_example")


@app.route("/")
Expand Down
2 changes: 1 addition & 1 deletion docs/sanic/streaming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Sanic allows you to get request data by stream, as below. When the request ends,
from sanic.response import stream, text

bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream')
app = Sanic(__name__)


class SimpleView(HTTPMethodView):
Expand Down
2 changes: 1 addition & 1 deletion docs/sanic/websocket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To setup a WebSocket:
from sanic.response import json
from sanic.websocket import WebSocketProtocol

app = Sanic()
app = Sanic("websocket_example")

@app.websocket('/feed')
async def feed(request, ws):
Expand Down