sanic/docs/sanic/getting_started.md

56 lines
1.3 KiB
Markdown
Raw Normal View History

2016-10-14 12:51:08 +01:00
# Getting Started
Make sure you have both [pip](https://pip.pypa.io/en/stable/installing/) and at
2019-05-14 09:21:24 +01:00
least version 3.6 of Python before starting. Sanic uses the new `async`/`await`
syntax, so earlier versions of python won't work.
2016-10-14 12:51:08 +01:00
## 1. Install Sanic
2019-05-15 07:54:02 +01:00
```bash
pip3 install sanic
```
To install sanic without `uvloop` or `ujson` using bash, you can provide either or both of these environmental variables
using any truthy string like `'y', 'yes', 't', 'true', 'on', '1'` and setting the `SANIC_NO_X` (`X` = `UVLOOP`/`UJSON`)
to true will stop that features installation.
```bash
SANIC_NO_UVLOOP=true SANIC_NO_UJSON=true pip3 install sanic
```
2019-05-15 07:54:02 +01:00
You can also install Sanic from [`conda-forge`](https://anaconda.org/conda-forge/sanic)
```bash
conda config --add channels conda-forge
conda install sanic
```
## 2. Create a file called `main.py`
2016-10-14 12:51:08 +01:00
```python
from sanic import Sanic
from sanic.response import json
2016-10-14 12:51:08 +01:00
app = Sanic()
2016-10-14 12:51:08 +01:00
@app.route("/")
async def test(request):
return json({"hello": "world"})
2016-10-14 12:51:08 +01:00
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)
```
## 3. Run the server
```
python3 main.py
```
## 4. Check your browser
Open the address `http://0.0.0.0:8000` in your web browser. You should see
the message *Hello world!*.
You now have a working Sanic server!