Skip to content

Instantly share code, notes, and snippets.

john = {
'name' : 'John',
'hair' : 'brown',
'from' : 'PA',
'limbs' : 4,
'colors' : ['green','brown','orange']
}
fed = {
'name' : 'Federico',
@johnschimmel
johnschimmel / itp_dwd_week_2_classes.py
Created September 13, 2012 10:47
Create a list full of Astronauts (objects)
class Astronaut(object):
# properties/atrributes/variables describe the object
# __init__ automatically called when creating an object
def __init__(self, nameStr = None):
print "Creating new astronaut:", nameStr
self.name = nameStr
# VERB sets a attribute
def set_name(self, nameStr):
@johnschimmel
johnschimmel / sample_express.js
Created September 13, 2012 21:17
updated expressjs + mongodb session code
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options',{layout:true}); // use /views/layout.html to manage your main header/footer wrapping template
app.register('html',require('ejs')); //use .html files in /views instead .hbs
app.use(express.cookieParser());
app.use(express.bodyParser());
@johnschimmel
johnschimmel / models.py
Created October 9, 2012 14:28
example model with mongoengine and wtforms
# -*- coding: utf-8 -*-
from mongoengine import *
from flask.ext.mongoengine.wtf import model_form
from datetime import datetime
class Comment(EmbeddedDocument):
name = StringField()
comment = StringField()
timestamp = DateTimeField(default=datetime.now())
@johnschimmel
johnschimmel / sample_route.py
Created October 9, 2012 14:32
Sample flask route for form display and form validation
# this is our main page
@app.route("/", methods=['GET','POST'])
def index():
# get Idea form from models.py
idea_form = models.IdeaForm(request.form)
# if form was submitted and it is valid...
if request.method == "POST" and idea_form.validate():
@johnschimmel
johnschimmel / sample_form.html
Last active January 20, 2016 12:58
sample form with flask-mongoengine wtform validation
<form method="POST" role="form">
{{ form.csrf_token }}
<legend><h3>Share Your Ideas</h3></legend>
{% if form.errors %}
<ul class="errors">
{% for field_name, field_errors in form.errors|dictsort if field_errors %}
{% for error in field_errors %}
<li>{{ form[field_name].label }}: {{ error }}</li>
@johnschimmel
johnschimmel / sample_route.py
Created October 21, 2012 19:48
Dictionary in dictionary to Jinja2 example
# put this in your app.py
@app.route("/nuts")
def nutbutter():
# nuts and review
nuttbutterReviews = {
'almond' : 'great',
'peanut' : 'okay',
'walnut' : 'what is this stuff?'
@johnschimmel
johnschimmel / sample_route.py
Created October 25, 2012 13:53
Sample of Flask receiving JSON
# If /json route receives header "application/json"
@app.route("/json", methods=['GET','POST'])
def json():
app.logger.debug("JSON received...")
app.logger.debug(request.json)
if request.json:
mydata = request.json # will be
@johnschimmel
johnschimmel / date_example.html
Created October 25, 2012 15:20
Example of getting Date and Time from HTML5 form
{% extends "layout/main.html" %}
{% block body %}
<div class="row">
<div class="span6">
<form method="POST">
<label for="date">Pick a date</label>
<input type="date" name="date" id="date">
@johnschimmel
johnschimmel / models.py
Created October 29, 2012 23:26
updated course units field
# -*- coding: utf-8 -*-
from mongoengine import *
from flask.ext.mongoengine.wtf import model_form
from datetime import datetime
class Comment(EmbeddedDocument):
name = StringField(required=False)
comment = StringField()