Advertisement
WupEly

Untitled

Mar 27th, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. from flask import Flask, Response
  2. import sqlite3
  3. from operator import itemgetter
  4.  
  5. app = Flask(__name__)
  6.  
  7.  
  8. @app.route("/schedule")
  9. def schcedule_route():
  10. return get_data()
  11.  
  12.  
  13. def get_data():
  14. with open("data.txt", "r") as read_file:
  15. file = read_file.read().split("\n")
  16. db_path = file[0]
  17. year = file[1]
  18.  
  19. conn = sqlite3.connect(db_path)
  20. c = conn.cursor()
  21. c.execute("""SELECT * FROM spaceships
  22. WHERE year = ?""", (year,))
  23. result = []
  24. for el in c.fetchall():
  25. result.append({
  26. "id": el[0],
  27. "name": el[1],
  28. "load": el[2],
  29. "crew": el[3],
  30. "year": el[4]
  31. })
  32. result.sort(key=itemgetter("load", "name"))
  33. c.close()
  34. conn.close()
  35. return result
  36.  
  37.  
  38. app.run()
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement