Skip to content

Instantly share code, notes, and snippets.

@aminamid
Created April 25, 2014 03:33
Show Gist options
  • Save aminamid/11277014 to your computer and use it in GitHub Desktop.
Save aminamid/11277014 to your computer and use it in GitHub Desktop.
Sample of AngularJS and Flask-Restless

Directory

model.py static/index.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-cookies.min.js"></script>
<script language="javascript">
function pausecomp(ms) {
ms += new Date().getTime();
while (new Date() < ms){}
}
angular.module('myApp', ['ngResource','ngCookies'])
.controller("myCtrl", ['$scope','$resource','$cookies',function($scope, $resource, $cookies) {
var mybucket = $resource("api/mybucket/:key",{},{
'query': {method:'GET', isArray:false }
});
mybucket.query({},function(response){$scope.hosts = response.objects; console.log(response.objects)});
$scope.addItem = function() {
console.log("submitted");
console.log($scope.newhost);
mybucket.save($scope.newhost);
mybucket.query({},function(response){$scope.hosts = response.objects;});
};
$scope.delItem = function() {
if (!confirm("Are you sure you want do delete it?")) return;
mybucket.delete($scope.delhost);
mybucket.query({},function(response){$scope.hosts = response.objects;});
};
}]);
</script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller='myCtrl'>
<div>
<form ng-submit="addItem()">
<input type="text" class="input-small" ng-model='newhost.key' placeholder="key">
<input type="number" class="input-small" ng-model='newhost.id' placeholder="id">
<input type="text" class="input-small" ng-model='newhost.name' placeholder="name">
<button type="submit" class="btn">add</button>
</form>
</div>
<div>
<form ng-submit="delItem()">
<select ng-model="delhost" ng-options="host.key for host in hosts"></select>
now selected {{ delhost }}
<button type="submit" class="btn">delete</button>
</form>
<hr>
<pre>{{hosts|json}}</pre>
</div>
</div>
</div>
</body>
</html>
import flask
import flask.ext.sqlalchemy
import flask.ext.restless
app = flask.Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
@app.route('/')
def index():
return app.send_static_file("index.html")
db = flask.ext.sqlalchemy.SQLAlchemy(app)
class Mybucket(db.Model):
id = db.Column(db.Integer)
key = db.Column(db.Unicode, unique=True, primary_key=True)
name = db.Column(db.Unicode)
# Create the database tables.
db.create_all()
# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Mybucket, methods=['GET', 'POST', 'DELETE'])
if __name__ == '__main__':
# start the flask loop
app.run(host='127.0.0.1',port=8888,debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment