diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2014-06-07 22:13:29 +0300 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2014-06-07 22:13:29 +0300 |
| commit | fd770a171d1363b163843b0950cbb179d80753b2 (patch) | |
| tree | b2f70c683a95208141a06860f9cd0a8ca8a7708f | |
| parent | 7e75dfdd983abf915b4201c99b16d60cd5419101 (diff) | |
Initial text only version
| -rw-r--r-- | server.py | 47 | ||||
| -rw-r--r-- | static/css/main.css | 15 | ||||
| -rw-r--r-- | templates/home.html | 142 |
3 files changed, 51 insertions, 153 deletions
@@ -2,6 +2,7 @@ from datetime import datetime from flask import Flask from flask import json, jsonify, render_template, request from unipath import FSPath as Path +from uuid import uuid4 DATA_DIR = Path(__file__).absolute().ancestor(1).child('data') @@ -11,48 +12,18 @@ app = Flask(__name__) def home(): return render_template('home.html') -@app.route('/sequence', methods=['GET']) -@app.route('/sequence/<name>', methods=['POST', 'GET']) -def sequence(name=None): - sequences_dir = DATA_DIR.child('sequences') - - if name and request.method == 'GET': - with open(sequences_dir.child('{}.json'.format(name)), 'r') as f: - return jsonify(json.load(f)) - - sequences = [{ - 'name': sequence.name.split('.json')[0], - 'pattern': json.load(open(sequence, 'r')) - } for sequence in sequences_dir.walk(pattern='*.json')] - - if request.method == 'POST': - with open(sequences_dir.child('{}.json'.format(name)), 'w') as f: - json.dump(data, f, indent=2) - - return render_template('sequence.html', sequences=sequences) - -def save_json(data, filename): - with open(DATA_DIR.child(filename), 'w') as f: - json.dump(data, f, indent=2) - -def save_raw(data, filename): - with open(DATA_DIR.child(filename), 'w') as f: - f.write('{}\n\n'.format(data['tempo'])) - for tree in data['trees']: - for row in tree: - f.write(''.join(map(str, row)) + '\n') - f.write('\n') - @app.route('/data', methods=['POST', 'GET']) def data(): if request.method == 'GET': - with open(DATA_DIR.child('data.json'), 'r') as f: - return jsonify(json.load(f)) + with open(DATA_DIR.child('data.txt'), 'r') as f: + return jsonify({ + 'data': f.read() + }) elif request.method == 'POST': - ts = datetime.now().strftime('%Y%m%d_%H%M%S') - save_json(request.json, 'data.json') - save_json(request.json, 'data.{}.json'.format(ts)) - save_raw(request.json, 'data.txt') + with open(DATA_DIR.child('data.txt'), 'w') as f: + f.write(request.json['data']) + with open(DATA_DIR.child('data.{}.txt'.format(uuid4().hex[:10])), 'w') as f: + f.write(request.json['data']) return 'ok' if __name__ == '__main__': diff --git a/static/css/main.css b/static/css/main.css index c994d08..e69de29 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -1,15 +0,0 @@ -.speed-slider { - margin: 40px 0px 10px 0px; -} - -.light-tree { - display: none; -} - -.light-cell-btn { - margin: 2px; -} - -.light-cell-btn.active { - background-color: #0F340D; -} diff --git a/templates/home.html b/templates/home.html index bdfee73..61a3beb 100644 --- a/templates/home.html +++ b/templates/home.html @@ -9,129 +9,71 @@ <script src="/static/js/jquery-2.1.1.min.js"></script> <script src="/static/js/underscore-min.js"></script> <script src="/static/js/bootstrap.min.js"></script> - <script src="/static/js/bootstrap-slider.min.js"></script> <script> function load_data() { - $.get('/data', function(res) { - console.log('Loading: ', res); - $('#speed-slider').slider({ - tooltip: 'always', - value: res.tempo - }); - _.each(res.trees, function(tree, i) { - var $tree = $('div[data-light-tree=' + i + ']'); - _.each(tree, function(channel, j) { - var $channel = $tree.find('div[data-light-channel=' + j + ']'); - _.each(channel, function(cell, k) { - var $cell = $channel.find('button[data-light-cell=' + k + ']'); - if (cell) { - $cell.addClass('active'); - } - }); - }); - }); - }); }; function save_data(data) { - var data = { - tempo: +$('#speed-slider').val(), - trees: [] - }; - - $('.light-tree').each(function(_idx, tree) { - tree = $(tree); - var tree_data = [] - tree.find('.light-row').each(function(_idx, row) { - var vals = $(row).find('.light-cell-btn').toArray().map(function(x) { - return x.className.contains('active') ? 1 : 0; - }); - tree_data.push(vals); - }); - data.trees.push(tree_data); - }); - - console.log('Saved:', data); - $.ajax({ - type: 'POST', - contentType: 'application/json', - data: JSON.stringify(data), - dataType: 'json', - url: '/data', - }); } $(document).ready(function() { - var TREES = 8; - var CHANNELS = 8; - var CELLS = 24; - - _.each(_.range(CHANNELS), function(i) { - var row = $(_.template($('#light-row-template').text(), {i: i, channels: CHANNELS, cells: CELLS})); - $('#light-container').append(row); - $('#tree-btns').append($('<button type="button" class="btn btn-default tree-btn" data-tree="' + i + '">Tree ' + i + '</button>')); - }); - - $('.tree-btn').click(function() { - var tree = $(this).attr('data-tree'); - $('.light-tree').hide(); - $('.light-tree[data-light-tree=' + tree +']').show(); - }); - - $('.toggle-row-btn').click(function() { - var row = $(this).parent('.light-row'); - $(row).find('.light-cell-btn').toggleClass('active'); - }) - - $('.clear-row-btn').click(function() { - var row = $(this).parent('.light-row'); - $(row).find('.light-cell-btn').removeClass('active'); - }) - - $('#save-btn').click(function(){ - save_data(); + $('#save-btn').click(function() { + $.ajax({ + type: 'POST', + contentType: 'application/json', + data: JSON.stringify({ + "data": $('#sequences-area').val() + '\n\n' + $('#composition-area').val() + }), + dataType: 'json', + url: '/data', + }); }); - - load_data(); - $('.light-tree[data-light-tree=0]').show(); }); </script> </head> <body> <div class="container"> <h1>Light Loop Pi</h1> - - <div class="speed-slider"> - <label>Loop speed:</label> - <input id="speed-slider" data-slider-id='ex1Slider' type="text" data-slider-min="500" data-slider-max="5000" data-slider-step="100"/> - </div> - <div class="btn-toolbar" role="toolbar"> - <div id="tree-btns" class="btn-group"></div> <div class="btn-group"> <button id="save-btn" type="button" class="btn btn-primary">Save</button> </div> </div> + + <div class="row"> + <div class="col-md-6"> + <h2>Sequences</h2> + <textarea id="sequences-area" cols="60" rows="20"></textarea> + <h4>Sequence Format</h4> + <pre> +# sequence_name +000000000000111111111111 +010101010101010101010101 +111111111111111111111111 +111111111111000000000000 +000000000000111111111111 +010101010101010101010101 +111111111111111111111111 +111111111111000000000000 + </pre> + <p>Rules: no spaces in name, same number of rows, all rows same length</p> + </div> + <div class="col-md-6"> + <h2>Composition</h2> + <textarea id="composition-area" cols="80" rows="20"></textarea> + <h4>Composition Format</h4> + <pre> +# composition +blinking,blinking,off,off +on,on,blinking,on +on,off,fade,fade - <div id="light-container"></div> - </div> - - <script type="text/template" class="template" id="light-row-template"> - <div class="light-tree" data-light-tree="<%= i %>"> - <h3>Tree <%= i %></h3> - <% _.each(_.range(channels), function(j) { %> - <div class="light-row" data-light-channel="<%= j %>"> - <span>Channel <%= j %></span> - <% _.each(_.range(cells), function(k) { %> - <button type="button" class="btn btn-success light-cell-btn" data-toggle="button" data-light-cell="<%= k %>"> </button> - <% }); %> - <button type="button" class="btn btn-warning toggle-row-btn">Toggle</button> - <button type="button" class="btn btn-warning clear-row-btn">Clear</button> - </div> - <% }); %> + </pre> + <p>Rules: all rows same length of sequences</p> + </div> </div> - </script> + </div> </body> </html> |
