Skip to content

Instantly share code, notes, and snippets.

@pawl
pawl / override_get_query.py
Created September 10, 2014 18:58
Override get_query based on GET parameter - Flask-Admin
from application import db
from application.views.modelview import ModelView
from application.models import Things # has an attribute called thing_type
class MyModelView(ModelView):
def get_query(self):
thing_type = request.args.get('type', None) # pretending we have a GET parameter called "type"
if thing_type == "type1":
return super(ModelView, self).get_query().filter(Things.thing_type.like('type1 %'))
elif thing_type == "type2":
@pawl
pawl / flask_memory_leak.py
Created October 13, 2014 16:20
Flask isn't releasing memory used for large dictionaries
import random
import optparse
from flask import redirect, Flask
from memory_profiler import memory_usage
# Create flask app
app = Flask(__name__)
app.debug = True
@pawl
pawl / flask_large_dict.py
Last active October 29, 2017 09:25
Trying to figure out how to release a large dictionary from memory...
import random
import optparse
import gc
from flask import redirect, Flask
from memory_profiler import memory_usage
# Create flask app
app = Flask(__name__)
app.debug = True
@pawl
pawl / gist:95769724848269cff890
Last active July 24, 2019 01:34
Solution to large dictionaries not released to memory: Run a separate process
import random
import optparse
import gc
from flask import redirect, Flask
from memory_profiler import memory_usage
from multiprocessing import Process
# Create flask app
app = Flask(__name__)
@pawl
pawl / gist:f7d58208cbeddb8c3b69
Last active August 29, 2015 14:08
New Machine Setup For Flask Development On Ubuntu
sudo apt-get update && time sudo apt-get dist-upgrade
sudo timedatectl set-timezone America/Chicago
apt-get install git fail2ban htop nano
mkdir -p .ssh
nano .ssh/authorized_keys
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
apt-get install python-setuptools
easy_install pip
pip install virtualenvwrapper
@pawl
pawl / hook.php
Last active August 29, 2015 14:09
An example of the ClientAdd WHMCS hook for Brandon
<?php
function send_invitiation_email($vars) {
$command = "sendemail";
$adminuser = "secret";
$values["messagename"] = "Google Groups Invitation";
$values["id"] = $vars['userid'];
$results = localAPI($command, $values, $adminuser);
@pawl
pawl / redirect.py
Last active October 10, 2018 20:09
Redirecting Within A Helper Function - Flask
from flask import Flask, redirect
from werkzeug.routing import RoutingException, HTTPException
app = Flask(__name__)
@app.route('/')
def hello_world():
def helper_function():
# Attempt #1
# this obviously won't work: return redirect('www.google.com')
from flask import Flask, request
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin.contrib import sqla
from flask.ext.admin import expose, Admin
app = Flask(__name__)
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
<!doctype html>
<html>
<head>
<title>Single Unchecked Checkbox Form Demo</title>
</head>
<body>
<form method=post action="/">
{{ form.uncheck_me.label }}
{{ form.uncheck_me }}
<br><input type=submit value=Submit>
from flask import Flask, request
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin.contrib import sqla
from flask.ext.admin import expose, Admin
app = Flask(__name__)
app.config['DATABASE_FILE'] = 'sample_db.sqlite'