로메오의 블로그

[Firebase] Python Flask 웹 서비스 본문

Frontend/Firebase

[Firebase] Python Flask 웹 서비스

romeoh 2019. 7. 21. 03:28
반응형

Firebase 목록

 

[FIREBASE] 호스팅 생성하고 배포하기

[Firebase] Firebase 데이터 베이스 생성 [Realtime Database]

[Firebase] Python - Firebase Realtime Database

app.py

import pyrebase
from flask import *

config = {
    "apiKey": "AIzaSyAsDlGTnwNoRgAdcoMV41x_xxxxx",
    "authDomain": "pythontest-xxxx.firebaseapp.com",
    "databaseURL": "https://pythontest-xxxx.firebaseio.com",
    "projectId": "pythontest-xxxx",
    "storageBucket": "",
    "messagingSenderId": "68980739xxxx",
    "appId": "1:689807391396:web:cedfcfbb5xxxxx"
}

firebase = pyrebase.initialize_app(config)

db = firebase.database()

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def main():
    return 'hello world'

if __name__ == '__main__':
    app.run(debug=True)

Flask 구동

$ python3 app.py

template 생성

$ mkdir templates
$ touch templates/index.html

index.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="/" method="post">
            <input type="text" name="name">
            <button type="submit">Submit</button>
        </form>
        {{ todo }}
    </body>
</html>

app.py

import pyrebase
from flask import *

config = {
    ...
}

firebase = pyrebase.initialize_app(config)

db = firebase.database()

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def main():
    if request.method == 'POST':
        name = request.form['name']
        db.child('todo').push(name)
        todo = db.child('todo').get()
        todo_list = todo.val()
        return render_template('index.html', todo=todo_list)
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

for문 사용하기

app.py

import pyrebase
from flask import *

config = {
    ...
}

firebase = pyrebase.initialize_app(config)

db = firebase.database()

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def main():
    if request.method == 'POST':
        name = request.form['name']
        db.child('todo').push(name)
        todo = db.child('todo').get()
        todo_list = todo.val()
        return render_template('index.html', todo=todo_list.values())
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

todo=todo_list.values() 로 수정합니다.

 

templates/index.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="/" method="post">
            <input type="text" name="name">
            <button type="submit">Submit</button>
        </form>
        {% for task in todo %}
            {{ task }}
        {% endfor %}
    </body>
</html>

 

walk를 입력했습니다.

 

 

Firebase 목록

반응형
Comments