Reputation: 11
I have created a flask app. To store the data I have used sqlalchemy to create a db file. My aim is to create an event calendar. I have decided to use FullCalendar.io . But the issue is that to feed the data it needs json file. Can anyone help me out
from flask import Flask, render_template, request, redirect
from flask.wrappers import Request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from sqlalchemy.orm import query
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///caldb.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class CalData(db.Model):
date = db.Column(db.String(200), nullable=False)
title = db.Column(db.String(200), nullable=False, primary_key=True)
desc = db.Column(db.String(500), nullable=False)
time = db.Column(db.Integer, nullable=False)
link = db.Column(db.String(200), nullable=True)
def __repr__(self) -> str:
return f"{self.title} - {self.time}"
@app.route('/cal2')
def cal2():
cd = CalData.query.all()
return render_template("cal2.html", data=cd)
Upvotes: 0
Views: 143
Reputation: 1386
I'm not familiar with FullCalendar.io, but let's assume that you need a list of objects, each representing a single calendar event, in the format you've described in the ORM model. Then, you only need to put all the data from the instances you get from your query into dict
s, and then send it out with jsonify
:
from flask import jsonify
...
...
...
@app.route('/cal2')
def cal2():
result = []
calendar_entries = CalData.query.all()
for entry in calendar_entries:
obj = dict()
obj['date'] = entry.date
obj['title'] = entry.title
...
result.append(obj)
return jsonify(result)
Upvotes: 1