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 ht
(obj)
| 13 | html_template = "<html><head></head><body><pre>%s</pre></body></html>" |
| 14 | |
| 15 | def view(obj): |
| 16 | """ |
| 17 | Allows viewing docstring with embedded images. |
| 18 | |
| 19 | To see a normal docstring, use help(object). |
| 20 | To see a docstring with embedded images, use docpicture.view(object). |
| 21 | It is assumed that the images are included (encoded in base 64) in |
| 22 | your Python module. |
| 23 | |
| 24 | The result will be an html file displayed in your default browser, |
| 25 | with the images inserted. |
| 26 | |
| 27 | For example: docpicture = python_powered_w.png |
| 28 | |
| 29 | Limitation: the filename must be a valid Python identifier. |
| 30 | Note that this works with gif images docpicture=python_g.gif |
| 31 | |
| 32 | as well as jpeg images docpicture = python_j.jpg |
| 33 | |
| 34 | All that is required is for the filename (without the extension) be a |
| 35 | unique value. Note that the same image can appear twice. |
| 36 | |
| 37 | docpicture = python_powered_w.png |
| 38 | """ |
| 39 | source_module = sys.modules[obj.__module__] |
| 40 | |
| 41 | docpicture_pattern = re.compile("\s*(docpicture\s*=\s*.+?)\s") |
| 42 | image_name_pattern = re.compile("\s*docpicture\s*=\s*(.+?)\s") |
| 43 | |
| 44 | docstring = obj.__doc__ |
| 45 | image_filename = image_name_pattern.search(obj.__doc__) |
| 46 | while image_filename is not None: |
| 47 | filename = image_filename.groups()[0] |
| 48 | base_name, ext = filename.split('.') |
| 49 | image = getattr(source_module, base_name).decode("base64") |
| 50 | |
| 51 | image_file = open(filename, "wb") |
| 52 | image_file.write(image) |
| 53 | image_file.close() |
| 54 | docstring = docpicture_pattern.sub("</pre><img src=%s><pre>" % filename, |
| 55 | docstring, count=1) |
| 56 | image_filename = image_name_pattern.search(docstring) |
| 57 | |
| 58 | html_file = open("test.html", 'w') |
| 59 | html_file.write(html_template % docstring) |
| 60 | html_file.close() |
| 61 | url = os.path.join(os.getcwd(), "test.html") |
| 62 | webbrowser.open(url) |
| 63 | |
| 64 | python_powered_w = """\ |
| 65 | iVBORw0KGgoAAAANSUhEUgAAAEYAAAAcCAYAAADcO8kVAAAABHNCSVQICAgIfAhkiAAAABl0RVh0 |