Skip to content

Instantly share code, notes, and snippets.

@nsfmc
Created September 8, 2014 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsfmc/2a877c61f0f7da984ead to your computer and use it in GitHub Desktop.
Save nsfmc/2a877c61f0f7da984ead to your computer and use it in GitHub Desktop.
a plotdevice file for making stuff with the ka api
"""
ka design automation tools -
super rough, but this is a set of functions, documented at the bottom,
which let you get the text/names of topics/subjects/domains for parts of
our topic tree.
you need to get the topic tree, which you can do by running
curl -o ~/topictree.json http://www.khanacademy.org/api/v1/topictree
good luck!
"""
size(1024, 768)
topics = read("~/topictree.json")
# print topics.keys()
# get subjects
domains = topics['children']
# a list of the human readable subjects
domain_names = [t['title'] for t in domains]
def subjects_for_domain(slug):
subjects = [t['children'] for t in domains
if t['domain_slug'] == slug]
if subjects:
return subjects[0]
def topic_color_for(slug):
colors = {u'coach-res': '#5cd0b3',
u'new-and-noteworthy': '#5cd0b3',
u'computing': '#83c167',
u'science': '#c55f73',
u'talks-and-interviews': '#5cd0b3',
u'humanities': '#fc6255',
u'partner-content': '#5cd0b3',
u'economics-finance-domain': '#f0ac5f',
u'test-prep': '#9a72ac',
u'math': '#4fbad4'}
return colors.get(slug, '#5cd0b3')
def subject_color_for(slug):
colors = {
u'computing': '#699c52',
u'science': '#94424f',
u'humanities': '#cf5044',
u'partner-content': '#49a88f',
u'economics-finance-domain': '#c78d46',
u'test-prep': '#644172',
u'math': '#1c758a'
}
return colors.get(slug, '#49a88f')
def stripe_domain(domain):
#draw progressively darker stripes of a color with subjects in a domain
bgcolor = color(topic_color_for(domain))
font_size = 20
box_size = 54
darkening = 0.6
tint_quantity = darkening / len(subjects_for_domain(domain))
bgcolor.v -= darkening
for (i, s) in enumerate(subjects_for_domain(domain)):
align(CENTER)
font("proxima nova semibold", font_size)
bgcolor.v += tint_quantity
fill(bgcolor)
rect(0,box_size*i, 1024, box_size)
fill(1)
text(str(s['title']), 512, i * box_size + 34)
def color_domains():
# returns all domains colored in their respective domain colors
for d in domains:
fill(subject_color_for(d['domain_slug']))
align(CENTER)
text(d['title'], 150, 100)
line()
def get_topics(**kwargs):
""" gets a list of topics in a subject
**kwargs
- domain: restricts chosen subject to domain
- subject: returns topics within a given subject
"""
import random
domain_slugs = [t['slug'] for t in domains]
domain_slug = kwargs.get('domain', random.choice(domain_slugs))
# domain_title = TODODODODODODO
subject_slugs = [s['slug'] for s in subjects_for_domain(domain_slug)]
subject_slug = kwargs.get('subject', random.choice(subject_slugs))
# subject_title = TODODODODODODODO
# TODO: fix this to validate that args actually exist, only random
# choices are guaranteed to exist
subject = filter(lambda x: x['slug'] == subject_slug,
subjects_for_domain(domain_slug))
print domain_slug
print subject_slug
print len(subject[0]['children'])
return subject[0]['children']
pass
## print all domains (math/science/etc)
# for t in domains:
# print t['title']
## print all math subjects (separated by ' ')
# print " ".join([t['title'] for t in subjects_for_domain('math')])
## print topics in a seventh grade math
# for t in get_topics(domain='math', subject='cc-seventh-grade-math'):
# print t['title']
## print topics in a random math mission
# for t in get_topics(domain='math'):
# print t['title']
## print topics in a random domain
# for t in get_topics(domain='math'):
# print t['title']
# print subject/slug pairs for math
# for s in subjects_for_domain('math'):
# print "%s: %s" % (s.get('title'), s.get('slug'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment