Skip to content

Instantly share code, notes, and snippets.

@DanielEFrampton
Last active April 6, 2020 23:24
Show Gist options
  • Save DanielEFrampton/cf86fd66727a4a24a40dad883ef202d0 to your computer and use it in GitHub Desktop.
Save DanielEFrampton/cf86fd66727a4a24a40dad883ef202d0 to your computer and use it in GitHub Desktop.
Setting Up a Flask App with PostgreSQL

Setting Up a Flask App with a PostgreSQL Database

Single context app or app factory with multiple contexts?

Rails Equivalencies

rails routes :

from app_name import app 
app.url_map

Common functions in application_controller which load current user to store in session : register a function to run before a request through request hooks and store data in g (context global) dictionary variable.

Render method in a controller action :
return method in a view function or make_response() function

Redirect in a controller action :

from flask import redirect
return redirect(http://someotherurl.website.com)

View files (using ERB) : Templates (using Jinja2) in templates/ directory, rendered from the view function like this:

return render_template('index.html')

Which passes in variables as additional arguments:

return render_template('index.html', name='Daniel')

And interpolates variables in the template that look like this:

<h1>Hello, {{ name }}!</h1>

Invisible vs. visible ERB tags : Interpolated Jinja2 in double braces ({{ variable }}) or control structures like this: {% if user %} which are unique to Jinja2 and not pure Python, unlike how ERB allows pure Ruby

Rendering partials in ERB view files : Template inheritance using {% block template_name %} and {% endblock %} in the root template and {% extends "index.html" %} in the extending template.

Session access : pretty much the same when storing to session, but when retrieving from session use session.get('key_name')

Flash messages : In view function, flash('Your flash message), and in template, get_flashed_messages()

Database/ORM Equivalences

ActiveRecord : SQLAlchemy

rails db:migrate : Within a shell session,

from app import db
db.create_all()

Adding rows to the database:

user = User.new(name: 'Daniel', address: '1251 Dexter St.')
user.save

:

user = User(name='Daniel', address='1251 Dexter St.')
db.session.add(user)
db.session.commit

User.all : User.query.all()

User.where(name: 'Daniel') : User.query.filter_by(name='Daniel').all()

User.where(name: 'Daniel').first : User.query.filter_by(name='Daniel').first()

User.all.to_sql : str(User.query.all())

SQLAlchemy "query filters" (from "Flask Web Development," Ch. 5, table 5-5):

  • filter() Returns a new query that adds an additional filter to the original query
  • filter_by() Returns a new query that adds an additional equality filter to the original query
  • limit() Returns a new query that limits the number of results of the original query to the given number
  • offset() Returns a new query that applies an offset into the list of results of the original query
  • order_by() Returns a new query that sorts the results of the original query according to the given criteria
  • group_by() Returns a new query that groups the results of the original query according to the given criteria

SQLAlchemy "query executors" (from "Flask Web Development", Ch. 5, table 5-6):

  • all() Returns all the results of a query as a list
  • first() Returns the first result of a query, or None if there are no results
  • first_or_404() Returns the first result of a query, or aborts the request and sends a 404 error as response if there are no results
  • get() Returns the row that matches the given primary key, or None if no matching row is found
  • get_or_404() Returns the row that matches the given primary key. If the key is not found it aborts the request and sends a 404 error as response
  • count() Returns the result count of the query
  • paginate() Returns a Pagination object that contains the specified range of results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment