Skip to content

Instantly share code, notes, and snippets.

@rduplain
rduplain / app.py
Created October 24, 2011 17:02
Template helpers vs global context processing in Flask.
# Discussing Flask templating with Hazel|work on #pocoo.
# Mixing request args and view func args in a template.
# Re: http://pastebin.com/Jr8ZUb25
from flask import Flask, request, render_template
app = Flask(__name__)
@rduplain
rduplain / app.py
Created March 12, 2012 23:31
Use pure SQLAlchemy APIs with Flask. Integrate with Flask-SQLAlchemy.
# Using SQLAlchemy 0.7.5 and Flask-SQLAlchemy 0.15.
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery, _QueryProperty
from models import Base
SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
@rduplain
rduplain / dash.py
Last active April 22, 2020 17:59
Amazon Dash→LIFX Power
#!/usr/bin/env python
# Detect Amazon Dash button network broadcast, toggle LIFX power based on MAC.
#
# Usage, with Amazon Dash buttons connected to LAN WiFi (see below for setup):
#
# 0. Configure `BUTTONS` with MAC to LIFX selector & `LIFX` with token.
# 1. Configure network firewall to block WAN out from dash MAC.
# 2. Install dependencies with `pip install requests scapy` on Python 3.
# 3. Run as root.
#
@rduplain
rduplain / README.md
Last active November 28, 2019 00:17
Vagrantfiles for testing configuration management (Ansible).

Vagrantfiles for testing configuration management (Ansible).

Overview

Configuration management that runs over SSH, namely Ansible, benefits in having test machines available on the network. The enclosed Vagrantfiles provide multiple virtual machines to run as guests on the developer's machine. The guests appear as hosts on the LAN, providing a local ephemeral cloud.

The virtual machines here are similar, but different. One provides **Ubuntu

@rduplain
rduplain / nginx-site-include.conf
Last active November 8, 2019 21:55
Configuration files for qwerty.sh deployment on a single server.
# qwerty.sh nginx configuration
#
# gzip is enabled, but only used by clients requesting it:
#
# curl -H 'Accept-Encoding: gzip,deflate' -sSL qwerty.sh
server {
listen 80;
server_name qwerty.sh www.qwerty.sh;
@rduplain
rduplain / refresh-hosts.bash
Last active November 8, 2019 21:55
Refresh /etc/hosts file with unpublished IPv4 aliases.
#!/usr/bin/env bash
# Refresh /etc/hosts file with unpublished IPv4 aliases.
#
# Set /etc/hosts.alias with domain/alias pairs, one pair per line:
#
# example.com server-alias
# example.net another
#
# https://github.com/rduplain/hosts
@rduplain
rduplain / README.md
Created August 30, 2012 16:07
Flask-Script: demo passing in configuration file.

This demonstrates that you can configure a Flask application through Flask-Script, without having to create a Flask instance or deal with circular dependencies. Note that Flask-Script's Manager accepts a factory function in place of a Flask app object.

Running:

python manage.py runserver

gives "Hello, world!" on http://localhost:5000/, while running:

python manage.py runserver -c development.cfg
@rduplain
rduplain / Vagrantfile
Last active October 18, 2019 18:16
Simple FreeBSD vagrant.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.guest = :freebsd
config.vm.box = "freebsd/FreeBSD-11.3-RELEASE"
config.vm.box_version = "2019.07.05"
config.vm.box_check_update = false
@rduplain
rduplain / app.py
Created November 10, 2011 15:44
Customize Flask to select a template based on some criteria.
"Customize Flask to select a template based on some criteria."
import os
from flask import Flask, request, render_template
from flask.helpers import locked_cached_property
from jinja2 import FileSystemLoader, TemplateNotFound
# Import a detection utility from your project, not defined here.
# Takes a request object and returns True if browser is mobile.
@rduplain
rduplain / app.py
Created January 30, 2012 15:49
Serve a Flask app on a sub-url during localhost development.
"Serve a Flask app on a sub-url during localhost development."
from flask import Flask
APPLICATION_ROOT = '/spam'
app = Flask(__name__)
app.config.from_object(__name__)