Advertisement
Gamerkin

app.py new

May 2nd, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import sqlite3
  2. from flask import Flask, render_template
  3. from werkzeug.exceptions import abort
  4.  
  5. app = Flask(__name__)
  6.  
  7.  
  8. def get_db_connection(filename):
  9.     conn = sqlite3.connect(filename)
  10.     conn.row_factory = sqlite3.Row
  11.     return conn
  12.  
  13.  
  14. @app.route('/')
  15. def index():
  16.     conn = get_db_connection('database.db')
  17.     items = conn.execute('SELECT * FROM items').fetchall()
  18.     conn.close()
  19.     return render_template('index.html', items=items)
  20.  
  21.  
  22. def get_item(item_id):
  23.     conn = get_db_connection('database.db')
  24.     item = conn.execute('SELECT * FROM items WHERE id = ?', (item_id,)).fetchone()
  25.     conn.close()
  26.     if item is None:
  27.         abort(404)
  28.     return item
  29.  
  30. def get_positiona(company):
  31.     conn = get_db_connection('mydatabase.db')
  32.     item = conn.execute('SELECT * FROM documents WHERE company_to = ?', (company,)).fetchall()
  33.     conn.close()
  34.     return item
  35.  
  36. @app.route('/<int:item_id>')
  37. def item(item_id):
  38.     item = get_item(item_id)
  39.     pos = get_positiona(item['company'])
  40.     return render_template('item.html', item=item, pos=pos)
  41.  
  42.  
  43. if __name__ == '__main__':
  44.     app.run()
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement