Salut,
Je travaille avec Flask dans développement Web, coté back-end.
Le but est de faire des requetes sur postgresql et d'enregistrer le résultat dans un fichier .json
app.py
models.py
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import os from flask import Flask, request, jsonify from flask_sqlalchemy import SQLAlchemy import json app = Flask(__name__) app.config.from_object("config.DevelopmentConfig") app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) #to handle the database transactions. from models import Build @app.route("/") def hello(): return "Hello World!" @app.route("/getall") def get_all(): try: datas = Build.query.all() return jsonify([e.to_dict() for e in datas]) except Exception as e: return(str(e))
quand j'exécute localhost/getall, cela fonctionne et il m'affiche le résultat sous format json.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 class Build(db.Model, SerializerMixin): serialize_types = ( (WKBElement, lambda x: to_shape(x).to_wkt()), #(AnyOtherType, serialize) ) __tablename__ = 'batiment1' id_batiment = db.Column(db.Integer, primary_key=True) z = db.Column(db.Integer) geom = db.Column(Geometry(geometry_type='POLYGON', srid=4326)) classe= db.Column(db.String()) def __init__(self, z, geom, classe): self.z = z self.geom = geom self.classe=classe def __repr__(self): return '<id_batiment {}>'.format(self.id_batiment)
Question:
comment je peux exporter ou enregistrer ce resultat dans un fichier .json ??
Partager