def populate_examples(num_examples=None, category=None, shuffle=False, shuffle_seed=42, **kwargs): """Iterate through Altair examples and extract code""" examples = sorted(iter_examples_with_metadata(), key=itemgetter('name')) if category is not None: examples = [ex for ex in examples if ex['category'] == category] if shuffle: random.Random(shuffle_seed).shuffle(examples) if num_examples is not None: examples = examples[:num_examples] for prev_ex, example, next_ex in prev_this_next(examples): try: code = Chart.from_dict(example['spec']).to_altair() except Exception as e: warnings.warn('altair-gallery: example {0} produced an error:\n' '{1}\n{2}'.format(example['name'], type(e), str(e))) code = '# (Altair JSON conversion failed).\nChart()' example['code'] = code if prev_ex: example['prev_ref'] = "gallery_{name}".format(**prev_ex) if next_ex: example['next_ref'] = "gallery_{name}".format(**next_ex) example['filename'] = '{0}.rst'.format(example['name']) example.update(kwargs) return examples
def populate_examples(**kwargs): """Iterate through Altair examples and extract code""" examples = list(iter_examples_with_metadata()) for prev_ex, example, next_ex in prev_this_next(examples): try: code = Chart.from_dict(example['spec']).to_altair() except Exception as e: warnings.warn('altair-gallery: example {0} produced an error:\n' '{1}\n{2}'.format(example['name'], type(e), str(e))) code = '# (Altair JSON conversion failed).\nChart()' example['code'] = code if prev_ex: example['prev_ref'] = "gallery_{name}".format(**prev_ex) if next_ex: example['next_ref'] = "gallery_{name}".format(**next_ex) example['filename'] = '{0}.rst'.format(example['name']) example.update(kwargs) return examples
def make_images(image_dir, default_image, make_thumbnails=True): """Use nodejs to make images and (optionally) thumbnails""" can_save = Chart._png_output_available() if not can_save: warnings.warn('Node is not correctly configured: cannot save images.') if not os.path.exists(image_dir): os.makedirs(image_dir) # store hashes so that we know whether images need to be generated hash_file = os.path.join(image_dir, '_image_hashes.json') if os.path.exists(hash_file): with open(hash_file) as f: hashes = json.load(f) else: hashes = {} for example in iter_examples_with_metadata(): filename = example['name'] + '.png' image_file = os.path.join(image_dir, filename) # check whether image already exists spec = example['spec'] spec_hash = dict_hash(spec) hashes_match = not hashes.get(filename, '') == spec_hash if hashes_match or not os.path.exists(image_file) or hashes_match: if can_save: chart = Chart.from_dict(spec) try: print('-> saving {0}'.format(image_file)) chart.savechart(image_file) except CalledProcessError: warnings.warn('Node is not correctly configured: ' 'cannot save images.') can_save = False if not os.path.exists(image_file): shutil.copyfile(default_image, image_file) else: hashes[filename] = spec_hash elif not os.path.exists(image_file): shutil.copyfile(default_image, image_file) if make_thumbnails: params = example.get('galleryParameters', {}) thumb_file = os.path.join(image_dir, example['name'] + '-thumb.png') create_thumbnail(image_file, thumb_file, **params) # Save hashes so we know whether we need to re-generate plots if hashes: with open(hash_file, 'w') as f: json.dump(hashes, f)
def make_images(image_dir, default_image, make_thumbnails=True): """Use nodejs to make images and (optionally) thumbnails""" can_save = savechart_available() if not can_save: warnings.warn('Node is not correctly configured: cannot save images.') if not os.path.exists(image_dir): os.makedirs(image_dir) # store hashes so that we know whether images need to be generated hash_file = os.path.join(image_dir, '_image_hashes.json') if os.path.exists(hash_file): with open(hash_file) as f: hashes = json.load(f) else: hashes = {} for example in iter_examples_with_metadata(): filename = example['name'] + '.png' image_file = os.path.join(image_dir, filename) # check whether image already exists spec = example['spec'] spec_hash = dict_hash(spec) if hashes.get(filename, '') == spec_hash: continue if can_save: chart = Chart.from_dict(spec) try: print('-> saving {0}'.format(image_file)) savechart(chart, image_file) except NodeExecError: warnings.warn( 'Node is not correctly configured: cannot save images.') can_save = False if not os.path.exists(image_file): shutil.copyfile(default_image, image_file) else: hashes[filename] = spec_hash elif not os.path.exists(image_file): shutil.copyfile(default_image, image_file) if make_thumbnails: convert_pct = lambda x: 0.01 * int(x.strip('%')) params = example.get('galleryParameters', {}) zoom = params.get('backgroundSize', '100%') if zoom == 'contains': zoom = '100%' zoom = convert_pct(zoom) #position = params.get('backgroundPosition', '0% 0%') #if position == 'left': position = '0% 0%' #xoffset, yoffset = map(convert_pct, position.split()) thumb_file = os.path.join(image_dir, example['name'] + '-thumb.png') create_thumbnail(image_file, thumb_file, zoom=zoom) # Save hashes so we know whether we need to re-generate plots if hashes: with open(hash_file, 'w') as f: json.dump(hashes, f)
def make_images(image_dir, default_image, make_thumbnails=True): """Use nodejs to make images and (optionally) thumbnails""" can_save = savechart_available() if not can_save: warnings.warn('Node is not correctly configured: cannot save images.') if not os.path.exists(image_dir): os.makedirs(image_dir) # store hashes so that we know whether images need to be generated hash_file = os.path.join(image_dir, '_image_hashes.json') if os.path.exists(hash_file): with open(hash_file) as f: hashes = json.load(f) else: hashes = {} for example in iter_examples_with_metadata(): filename = example['name'] + '.png' image_file = os.path.join(image_dir, filename) # check whether image already exists spec = example['spec'] spec_hash = dict_hash(spec) if hashes.get(filename, '') == spec_hash: continue if can_save: chart = Chart.from_dict(spec) try: print('-> saving {0}'.format(image_file)) savechart(chart, image_file) except NodeExecError: warnings.warn('Node is not correctly configured: cannot save images.') can_save = False if not os.path.exists(image_file): shutil.copyfile(default_image, image_file) else: hashes[filename] = spec_hash elif not os.path.exists(image_file): shutil.copyfile(default_image, image_file) if make_thumbnails: convert_pct = lambda x: 0.01 * int(x.strip('%')) params = example.get('galleryParameters', {}) zoom = params.get('backgroundSize', '100%') if zoom == 'contains': zoom = '100%' zoom = convert_pct(zoom) #position = params.get('backgroundPosition', '0% 0%') #if position == 'left': position = '0% 0%' #xoffset, yoffset = map(convert_pct, position.split()) thumb_file = os.path.join(image_dir, example['name'] + '-thumb.png') create_thumbnail(image_file, thumb_file, zoom=zoom) # Save hashes so we know whether we need to re-generate plots if hashes: with open(hash_file, 'w') as f: json.dump(hashes, f)