Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import os
  2. import sqlite3
  3. from flask import Flask, request, session, g, redirect, url_for, abort, \
  4. render_template, flash
  5.  
  6. app = Flask(__name__) # create the application instance :)
  7. app.config.from_object(__name__) # load config from this file , flaskr.py
  8.  
  9. # Load default config and override config from an environment variable
  10. app.config.update(dict(
  11. DATABASE=os.path.join(app.root_path, 'flaskr.db'),
  12. SECRET_KEY='development key',
  13. USERNAME='admin',
  14. PASSWORD='default'
  15. ))
  16. app.config.from_envvar('FLASKR_SETTINGS', silent=True)
  17.  
  18. # string to find for regex replace of static files
  19. # ="../static/(.*?)"
  20. # string to replace
  21. # ="{{ url_for\('static', filename='$1'\) }}"
  22.  
  23. """ Database Connections """
  24.  
  25.  
  26. def connect_db():
  27. """Connects to the specific database."""
  28. rv = sqlite3.connect(app.config['DATABASE'])
  29. rv.row_factory = sqlite3.Row
  30. return rv
  31.  
  32.  
  33. def get_db():
  34. """Opens a new database connection if there is none yet for the
  35. current application context.
  36. """
  37. if not hasattr(g, 'sqlite_db'):
  38. g.sqlite_db = connect_db()
  39. return g.sqlite_db
  40.  
  41.  
  42. @app.teardown_appcontext
  43. def close_db(error):
  44. """Closes the database again at the end of the request."""
  45. if hasattr(g, 'sqlite_db'):
  46. g.sqlite_db.close()
  47.  
  48.  
  49. def init_db():
  50. db = get_db()
  51. with app.open_resource('schema.sql', mode='r') as f:
  52. db.cursor().executescript(f.read())
  53. db.commit()
  54.  
  55.  
  56. @app.cli.command('initdb')
  57. def initdb_command():
  58. """Initializes the database."""
  59. init_db()
  60. print('Initialized the database.')
  61.  
  62.  
  63. """ End Of Database Connections """
  64.  
  65.  
  66. @app.route('/')
  67. def index():
  68. db = get_db()
  69. #cur = db.execute('select title, text from entries order by id desc')
  70. #entries = cur.fetchall()
  71. entries = "test"
  72. return render_template('index.html', entries=entries)
  73.  
  74.  
  75.  
  76. # flask initdb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement