'''Function allowing to view docstrings with embedded images. The images are encoded in base 64. (c) 2008 Andre Roberge License: MIT License ''' import os import re import sys import webbrowser html_template = "<html><head></head><body><pre>%s</pre></body></html>" def view(obj): """ Allows viewing docstring with embedded images. To see a normal docstring, use help(object). To see a docstring with embedded images, use docpicture.view(object). It is assumed that the images are included (encoded in base 64) in your Python module. The result will be an html file displayed in your default browser, with the images inserted. For example: docpicture = python_powered_w.png Limitation: the filename must be a valid Python identifier. Note that this works with gif images docpicture=python_g.gif as well as jpeg images docpicture = python_j.jpg All that is required is for the filename (without the extension) be a unique value. Note that the same image can appear twice. docpicture = python_powered_w.png """ source_module = sys.modules[obj.__module__] docpicture_pattern = re.compile("\s*(docpicture\s*=\s*.+?)\s") image_name_pattern = re.compile("\s*docpicture\s*=\s*(.+?)\s") docstring = obj.__doc__ image_filename = image_name_pattern.search(obj.__doc__) while image_filename is not None: filename = image_filename.groups()[0] base_name, ext = filename.split('.') image = getattr(source_module, base_name).decode("base64") image_file = open(filename, "wb") image_file.write(image) image_file.close() docstring = docpicture_pattern.sub("</pre><img src=%s><pre>" % filename, docstring, count=1) image_filename = image_name_pattern.search(docstring) html_file = open("test.html", 'w') html_file.write(html_template % docstring) html_file.close() url = os.path.join(os.getcwd(), "test.html") webbrowser.open(url) python_powered_w = """\ iVBORw0KGgoAAAANSUhEUgAAAEYAAAAcCAYAAADcO8kVAAAABHNCSVQICAgIfAhkiAAAABl0RVh0 U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAgRSURBVFjD7ZmJU1vHGcDzV2nG6dTpdOJ2 EsfFtZmkR9pxEo+dCcRgbgYM4bIhOCCMbS4hLoPFaRCYcjkxbbkloVtI6L4lBOgwTwcSX9+HeRSj w4pIitOxZr7Rvt19+3Z/+1373jsAwHkr4fLOL30BgUCAe+pgArtB7vcCneLpskr5dFl5IOvKp0vr Sp3dKfxfQ9Hr9eLc3FzvqYKhs3mmc4X9AdrNLqCld5LSDrS0NqDdaAXa1ww4c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