Update docs with changes done in 20.3 (#1822)

* Remove raw_args from docs (deprecated feature removed in Sanic 20.3).

* Add missing Sanic(name) arguments in docs. Merge async/non-async class view examples.

Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
This commit is contained in:
L. Kärkkäinen 2020-03-31 20:57:09 +03:00 committed by GitHub
parent aa6ea5b5a0
commit 78e912ea45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 25 additions and 44 deletions

View File

@ -28,14 +28,15 @@ using all these methods would look like the following.
from sanic.views import HTTPMethodView from sanic.views import HTTPMethodView
from sanic.response import text from sanic.response import text
app = Sanic('some_name') app = Sanic("class_views_example")
class SimpleView(HTTPMethodView): class SimpleView(HTTPMethodView):
def get(self, request): def get(self, request):
return text('I am get method') 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') return text('I am post method')
def put(self, request): def put(self, request):
@ -49,22 +50,6 @@ using all these methods would look like the following.
app.add_route(SimpleView.as_view(), '/') 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 URL parameters
-------------- --------------
@ -154,7 +139,7 @@ lambda:
from sanic.views import CompositionView from sanic.views import CompositionView
from sanic.response import text from sanic.response import text
app = Sanic(__name__) app = Sanic("composition_example")
def get_handler(request): def get_handler(request):
return text('I am a get method') return text('I am a get method')

View File

@ -39,13 +39,13 @@ Any variables defined with the `SANIC_` prefix will be applied to the sanic conf
.. code-block:: python .. 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: 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 .. code-block:: python
app = Sanic(load_env=False) app = Sanic(__name__, load_env=False)
From an Object From an Object
~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

View File

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

View File

@ -59,7 +59,7 @@ You can also add an exception handler as such:
async def server_error_handler(request, exception): async def server_error_handler(request, exception):
return text("Oops, server error", status=500) return text("Oops, server error", status=500)
app = Sanic() app = Sanic("error_handler_example")
app.error_handler.add(Exception, server_error_handler) app.error_handler.add(Exception, server_error_handler)
In some cases, you might want to add some more error handling In some cases, you might want to add some more error handling
@ -77,7 +77,7 @@ can subclass Sanic's default error handler as such:
# You custom error handling logic... # You custom error handling logic...
return super().default(request, exception) return super().default(request, exception)
app = Sanic() app = Sanic("custom_error_handler_example")
app.error_handler = CustomErrorHandler() app.error_handler = CustomErrorHandler()
Useful exceptions Useful exceptions

View File

@ -37,7 +37,7 @@ You can also install Sanic from `conda-forge <https://anaconda.org/conda-forge/s
from sanic import Sanic from sanic import Sanic
from sanic.response import json from sanic.response import json
app = Sanic() app = Sanic("hello_example")
@app.route("/") @app.route("/")
async def test(request): async def test(request):

View File

@ -15,7 +15,7 @@ Sanic aspires to be simple
from sanic import Sanic from sanic import Sanic
from sanic.response import json from sanic.response import json
app = Sanic() app = Sanic("App Name")
@app.route("/") @app.route("/")
async def test(request): async def test(request):

View File

@ -17,7 +17,7 @@ A simple example using default settings would be like this:
from sanic.log import logger from sanic.log import logger
from sanic.response import text from sanic.response import text
app = Sanic('test') app = Sanic('logging_example')
@app.route('/') @app.route('/')
async def test(request): async def test(request):
@ -47,7 +47,7 @@ initialize ``Sanic`` app:
.. code:: python .. 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: And to close logging, simply assign access_log=False:
@ -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`` 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

View File

@ -138,7 +138,7 @@ the one you instantiate your app in.
.. code-block:: python .. code-block:: python
app = Sanic() app = Sanic(__name__)
async def setup_db(app, loop): async def setup_db(app, loop):
app.db = await db_setup() app.db = await db_setup()

View File

@ -32,7 +32,7 @@ and in Nginx config.
from sanic import Sanic from sanic import Sanic
from sanic.response import text from sanic.response import text
app = Sanic("Sanic Example") app = Sanic("proxied_example")
app.config.FORWARDED_SECRET = "YOUR SECRET" app.config.FORWARDED_SECRET = "YOUR SECRET"
@app.get("/") @app.get("/")

View File

@ -56,7 +56,6 @@ The difference between Request.args and Request.query_args for the queryset `?ke
"url": request.url, "url": request.url,
"query_string": request.query_string, "query_string": request.query_string,
"args": request.args, "args": request.args,
"raw_args": request.raw_args,
"query_args": request.query_args, "query_args": request.query_args,
}) })
@ -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", "url":"http:\/\/0.0.0.0:8000\/test_request_args?key1=value1&key2=value2&key1=value3",
"query_string":"key1=value1&key2=value2&key1=value3", "query_string":"key1=value1&key2=value2&key1=value3",
"args":{"key1":["value1","value3"],"key2":["value2"]}, "args":{"key1":["value1","value3"],"key2":["value2"]},
"raw_args":{"key1":"value1","key2":"value2"},
"query_args":[["key1","value1"],["key2","value2"],["key1","value3"]] "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 - `files` (dictionary of `File` objects) - List of files that have a name, body, and type
.. code-block:: python .. code-block:: python
@ -206,7 +202,7 @@ The output will be:
Accessing values using `get` and `getlist` 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. 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 - `get(key, default=None)` operates as normal, except that when the value of
@ -228,14 +224,14 @@ The key difference when using this object is the distinction between the `get` a
from sanic import Sanic from sanic import Sanic
from sanic.response import json from sanic.response import json
app = Sanic(name="example") app = Sanic(__name__)
@app.route("/") @app.route("/")
def get_handler(request): def get_handler(request):
return json({ return json({
"p1": request.args.getlist("p1") "p1": request.args.getlist("p1")
}) })
Accessing the handler name with the request.endpoint attribute Accessing the handler name with the request.endpoint attribute
-------------------------------------------------------------- --------------------------------------------------------------
@ -247,7 +243,7 @@ route will return "hello".
from sanic.response import text from sanic.response import text
from sanic import Sanic from sanic import Sanic
app = Sanic() app = Sanic(__name__)
@app.get("/") @app.get("/")
def hello(request): def hello(request):

View File

@ -16,7 +16,7 @@ IPv6 example:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.bind(('::', 7777)) sock.bind(('::', 7777))
app = Sanic() app = Sanic("ipv6_example")
@app.route("/") @app.route("/")
@ -46,7 +46,7 @@ UNIX socket example:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(server_socket) sock.bind(server_socket)
app = Sanic() app = Sanic("unix_socket_example")
@app.route("/") @app.route("/")

View File

@ -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 from sanic.response import stream, text
bp = Blueprint('blueprint_request_stream') bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream') app = Sanic(__name__)
class SimpleView(HTTPMethodView): class SimpleView(HTTPMethodView):

View File

@ -12,7 +12,7 @@ To setup a WebSocket:
from sanic.response import json from sanic.response import json
from sanic.websocket import WebSocketProtocol from sanic.websocket import WebSocketProtocol
app = Sanic() app = Sanic("websocket_example")
@app.websocket('/feed') @app.websocket('/feed')
async def feed(request, ws): async def feed(request, ws):