Sanic 串連postgresql資料庫

來源:互聯網
上載者:User

標籤:sanic jinja2 peewee_async

1.安裝系統包

# yum install postgresql-devel

2.安裝Python包

peewee-2.8.5.tar.gz

psycopg2-2.6.2.tar.gz

1).安裝peewee-async

# pip install peewee-async

Collecting peewee-async

  Downloading peewee_async-0.5.6-py3-none-any.whl

Requirement already satisfied: peewee>=2.8.0 in /usr/local/lib/python3.5/site-packages (from peewee-async)

Installing collected packages: peewee-async

Successfully installed peewee-async-0.5.6

#

2).安裝aiopg

# pip install aiopg

Collecting aiopg

  Using cached aiopg-0.13.0-py3-none-any.whl

Requirement already satisfied: psycopg2>=2.5.2 in /usr/local/lib/python3.5/site-packages/psycopg2-2.6.2-py3.5-linux-x86_64.egg (from aiopg)

Installing collected packages: aiopg

Successfully installed aiopg-0.13.0


3.目錄結構

/home/webapp

     |-- main.py

     |-- my_blueprint.py

     templates

        |-- index.html



4.檔案內容:


1).main.py


# more main.py 

from sanic import Sanic

from my_blueprint import bp


app = Sanic(__name__)

app.blueprint(bp)


app.run(host=‘0.0.0.0‘, port=8000, debug=True)


2).my_blueprint.py


# more my_blueprint.py 

from sanic import Blueprint

from sanic.response import json, text, html


## Jinja2 template ####

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader(‘my_blueprint‘, ‘templates‘))


## database ####

import uvloop, peewee

from peewee_async import PostgresqlDatabase


bp = Blueprint(‘my_blueprint‘)


# init db connection

global database

database = PostgresqlDatabase(database=‘webdb‘,

                              host=‘127.0.0.1‘,

                              user=‘postgres‘,

                              password=‘111111‘)


# router define

@bp.route(‘/‘)

async def bp_root(request):

    serialized_obj = []

    cursor = database.execute_sql(‘select * from t1;‘)

    for row in cursor.fetchall():

         serialized_obj.append({

            ‘id‘: row[0],

            ‘name‘: row[1]}

        )

    template = env.get_template(‘index.html‘)

    content=template.render(items=serialized_obj)

    return html(content)


#


3).index.html


# more index.html 

<!doctype html>

<title> Sanic </title>

<div class=page>

  <table border="1" cellpadding="10">

  <tr>

    <th>id</th>

    <th>name</th>

  </tr>

  {% for item in items %}

    <tr>

    <td> {{ item.id }} </td>

    <td> {{ item.name }} </td> 

    </tr>

  {% endfor %}

  </table>

</div>


5.瀏覽器運行結果


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8B/76/wKiom1hOfM6CNvI6AABxnX5BLSc496.jpg-wh_500x0-wm_3-wmp_4-s_1855399289.jpg" title="sanic_db.JPG" alt="wKiom1hOfM6CNvI6AABxnX5BLSc496.jpg-wh_50" />









本文出自 “yiyi” 部落格,請務必保留此出處http://heyiyi.blog.51cto.com/205455/1882032

Sanic 串連postgresql資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.