From 66a8cac72e9d6c50089a7784adb11d3a285204c5 Mon Sep 17 00:00:00 2001 From: Jenna Zeigen Date: Tue, 27 Nov 2012 14:10:29 -0500 Subject: [PATCH 1/3] tooltips added --- README.md | 3 ++- reflection_scraper.py | 27 +++++++++++++++++++- request.py | 6 ++--- server.py | 28 ++++++++++++++++----- static/style.css | 38 ++++++++++++++++++++++++++++ static/tree.js | 58 ++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 146 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8dc33ce..bca9876 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ Misc. ---------- To run request.py or main.py, you need to set REFLECTION_ID to your HS login and REFLECTION_SECRET to your HS password in your environmental -variables. +variables. + Ignore ---------- diff --git a/reflection_scraper.py b/reflection_scraper.py index e137e8a..f39cedc 100644 --- a/reflection_scraper.py +++ b/reflection_scraper.py @@ -1,6 +1,7 @@ from bs4 import BeautifulSoup import os import re +import string #create_frequency_dict can't handle keywords with characters like #+, *, or ? that have special meanings in regex @@ -14,6 +15,30 @@ def get_keywords(filename): keywords.append(line.strip()) return keywords +extra_stops = ['code', 'planned', 'learn', 'learned', 'learning', 'will', 'continue', 'time', "i'm", 'today', 'tomorrow', 'yesterday'] + +def generate_popular(reflections_mapping, extra_stops): + '''input: mapping of names to reflections text + output: list of top 50 popular 'key' words''' + word_bucket = {} + file = open('stopword.txt', 'r') + dump = file.read() + file.close() + stopwords = dump.split() + for person in reflections_mapping: + words = reflections_mapping[person].strip().lower().split() + for word in words: + wordy = word.strip(string.punctuation+string.whitespace) + if stopwords.count(wordy) == 0: + if wordy in word_bucket: + word_bucket[wordy] += 1 + else: + word_bucket[wordy] = 1 + sorted = word_bucket.items() + sorted.sort(key=lambda x: x[1]) + sorted.reverse() + return sorted[0:50] + def get_file_names(dir_path): '''input: path to directory output: list of file names in directory''' @@ -77,4 +102,4 @@ def get_peoples_words(): reflections = get_reflections(file_names) reflections_dict = scrape_reflections(reflections) db_entries = create_db_entries(keywords, reflections_dict) - return db_entries + return db_entries \ No newline at end of file diff --git a/request.py b/request.py index 8faf986..b232957 100644 --- a/request.py +++ b/request.py @@ -29,9 +29,9 @@ def get_session(email, password, host='https://www.hackerschool.com'): def download_reflections_pages(): '''downloads all reflection pages and stores them in a folder called html''' host = 'https://hackerschool.com' - email = os.environ.get('REFLECTION_ID') - password = os.environ.get('REFLECTION_SECRET') - s = get_session(email, password) + # email = os.environ.get('REFLECTION_ID') + # password = os.environ.get('REFLECTION_SECRET') + s = get_session('jenna.zeigen@gmail.com', 'cataphora953') if not os.path.exists ("html"): os.mkdir ("html") diff --git a/server.py b/server.py index 0664006..2f87a9d 100644 --- a/server.py +++ b/server.py @@ -2,12 +2,17 @@ from pymongo import Connection import json import os +import re + +import reflection_scraper app = Flask(__name__) connection = Connection() db = connection.reflections collection = db.words +reflections_dict = {} + @app.route('/') def start(): return render_template('index.html') @@ -27,16 +32,12 @@ def get_icon(): #described above. def get_JSON(name): - - print "got request for " + name - match_data = {} match_data['name'] = name match_data['children'] = [] doc = collection.find_one({'name':name}) - print "name: " + name my_keywords = doc['keywords'].keys() for keyword in my_keywords: @@ -67,9 +68,24 @@ def get_JSON(name): match_data['children'].append(word_data) return json.dumps(match_data) + +@app.route('//') +def get_mentions(name, term): + global reflections_dict + if len(reflections_dict) == 0: + reflections_dict = get_ref_dict(name, term) + print reflections_dict + places = [m.start() for m in re.finditer(term, reflections_dict[name])] + snippets = [] + for loc in places: + snippets.append(['...' + reflections_dict[name][loc-100:loc+100] + '...']) + return json.dumps(snippets) + +def get_ref_dict(name, term): + return reflection_scraper.scrape_reflections(reflection_scraper.get_reflections(reflection_scraper.get_file_names('html/'))) if __name__ == '__main__': - port = int(os.environ.get('PORT', 5000)) - app.run(host='0.0.0.0', port=port) + port = int(os.environ.get('PORT', 5757)) + app.run(debug=True) diff --git a/static/style.css b/static/style.css index 73f8c42..51fbaf5 100644 --- a/static/style.css +++ b/static/style.css @@ -56,4 +56,42 @@ circle { font-size: 14px; letter-spacing: 1px; color: #574D37; +} + +.tooltip { + position: absolute; + background-color: white; + padding: 5px; + border-radius: 10px; + box-shadow: 0 0 15px 5px #E3E3E3; + width: 300px; + +} + +.close{ + width:15px; + height:15px; + border-radius:50px; + font-size:.75em; + font-family: sans-serif; + color: white; + text-align:center; + text-decoration:none; + background:red; + display: inline-block; + margin: 5px; + float: left; +} + +.close:hover { + background-color: rgb(175,10,10); + cursor: pointer; +} + +.hidden { + display: none; +} + +.not-option { + color: #999; } \ No newline at end of file diff --git a/static/tree.js b/static/tree.js index 115c74f..cf4144f 100644 --- a/static/tree.js +++ b/static/tree.js @@ -2,7 +2,6 @@ //see original at http://mbostock.github.com/d3/ex/tree.html var generate_tree = function(){ - console.log("entering generate tree") var radius = 960 / 2; var tree = d3.layout.tree() @@ -37,8 +36,18 @@ var generate_tree = function(){ .data(nodes) .enter().append("g") .attr("class", "node") - .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; }); - + .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; }) + .attr('id', function(d) { + if (d.parent){ + return d.name + '-'+ d.parent.name + } + else { + return d.name + } + + }) + .attr('title', function(d){return d.depth}) + node.append("circle") .attr("r", 4.5); @@ -48,6 +57,49 @@ var generate_tree = function(){ .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; }) .attr("transform", function(d) { return d.x < 180 ? "translate(8)" : "rotate(180)translate(-8)"; }) .text(function(d) { return d.name; }); + + $('.node').click(function(e) { + console.log(this) + var name = $.trim($(this).attr('id').split('-')[0].split('(')[0]) + var parent = $(this).attr('id').split('-')[1] + var closed = null; + //if something's already open + if ($('.tooltip').length > 0) { + closed = $('.tooltip').remove() + } + //if it doesn't belong to just clicked thing + if ((!closed) || (closed.attr('id') !== name + '-' + parent)){ + var depth = $(this).attr('title') + var x = e.pageX + var y = e.pageY-100 + if (depth == 2){ + $.get('/' + name + '/' + parent, function(data){ + var lines = $.parseJSON(data) + var currDisp = 0 + d3.select('#chart').append('div') + .style('top', y + 'px') + .style('left', x + 'px') + .attr('class', 'tooltip') + .attr('id', name + '-' + parent) + .html("x

" + lines[currDisp]+ "

Previous Next
") + + $('.close').click(function(){ + $(this).parent().remove() + }) + + $('#next').click(function() { + currDisp = Math.min(currDisp+1, lines.length-1) + $('#snip').text(lines[currDisp][0]) + }) + + $('#prev').click(function() { + currDisp = Math.max(currDisp-1, 0) + $('#snip').text(lines[currDisp][0]) + }) + }); + } + } + }) }); }; From 10c5a3a6d98dd50691f8ef2118101860d46f44ea Mon Sep 17 00:00:00 2001 From: Jenna Zeigen Date: Tue, 27 Nov 2012 14:40:28 -0500 Subject: [PATCH 2/3] double loading fixed --- static/tree.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/static/tree.js b/static/tree.js index cf4144f..6651854 100644 --- a/static/tree.js +++ b/static/tree.js @@ -2,6 +2,7 @@ //see original at http://mbostock.github.com/d3/ex/tree.html var generate_tree = function(){ + console.log('gen tree') var radius = 960 / 2; var tree = d3.layout.tree() @@ -59,7 +60,7 @@ var generate_tree = function(){ .text(function(d) { return d.name; }); $('.node').click(function(e) { - console.log(this) + console.log('click') var name = $.trim($(this).attr('id').split('-')[0].split('(')[0]) var parent = $(this).attr('id').split('-')[1] var closed = null; @@ -88,11 +89,13 @@ var generate_tree = function(){ }) $('#next').click(function() { + console.log('click next') currDisp = Math.min(currDisp+1, lines.length-1) $('#snip').text(lines[currDisp][0]) }) $('#prev').click(function() { + console.log('click prev') currDisp = Math.max(currDisp-1, 0) $('#snip').text(lines[currDisp][0]) }) @@ -106,13 +109,9 @@ var generate_tree = function(){ $(document).ready(function(){ $('#form').submit(function(){ + console.log('submit') generate_tree(); return false; }); - $('#name').keypress(function(e){ - if (e.keyCode===13) { - generate_tree(); - } - }); $('#name').focus() }); \ No newline at end of file From c764072e5703d4b5e45488a4df7ffb17bf9026c3 Mon Sep 17 00:00:00 2001 From: Jenna Zeigen Date: Tue, 27 Nov 2012 14:54:31 -0500 Subject: [PATCH 3/3] clean-up --- static/tree.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/static/tree.js b/static/tree.js index 6651854..5b07316 100644 --- a/static/tree.js +++ b/static/tree.js @@ -2,7 +2,6 @@ //see original at http://mbostock.github.com/d3/ex/tree.html var generate_tree = function(){ - console.log('gen tree') var radius = 960 / 2; var tree = d3.layout.tree() @@ -60,7 +59,6 @@ var generate_tree = function(){ .text(function(d) { return d.name; }); $('.node').click(function(e) { - console.log('click') var name = $.trim($(this).attr('id').split('-')[0].split('(')[0]) var parent = $(this).attr('id').split('-')[1] var closed = null; @@ -89,13 +87,11 @@ var generate_tree = function(){ }) $('#next').click(function() { - console.log('click next') currDisp = Math.min(currDisp+1, lines.length-1) $('#snip').text(lines[currDisp][0]) }) $('#prev').click(function() { - console.log('click prev') currDisp = Math.max(currDisp-1, 0) $('#snip').text(lines[currDisp][0]) }) @@ -109,7 +105,6 @@ var generate_tree = function(){ $(document).ready(function(){ $('#form').submit(function(){ - console.log('submit') generate_tree(); return false; });