Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
797 B
1
Indexable
Never
from flask import Flask, render_template, jsonify
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    body = db.Column(db.Text, nullable=False)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/data')
def get_data():
    start = int(request.args.get('start', 0))
    count = int(request.args.get('count', 10))
    posts = Post.query.order_by(Post.id.desc()).offset(start).limit(count).all()
    data = [{'title': post.title, 'body': post.body} for post in posts]
    return jsonify(data)

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