tommikaikkonen / prettyprinter

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
https://prettyprinter.readthedocs.io
MIT License
336 stars 20 forks source link

Overriding str pretty-printing (for both pprint and cpprint in Jupyter) #26

Closed patarapolw closed 5 years ago

patarapolw commented 6 years ago

Description

I want to insert hyper_markdownify(s) into your code, and plan to make text-to-speech (TTS) on click in Jupyter Notebook.

What I Did

In this repo: https://github.com/patarapolw/pyrepr

import json
from markdownify import markdownify

def hyper_markdownify(s):
    while True:
        try:
            s = json.loads(s)
        except (json.decoder.JSONDecodeError, TypeError):
            break

    return markdownify(s)

Most importantly, how do I extract the formatted HTML? I am able to extract the string using

pprint(obj, StringIO())

But I cannot do the same with cpprint.

tommikaikkonen commented 5 years ago

I'm not a 100% sure what you're asking, but prettyprinter is meant for pretty output of Python data, generally for humans to read—HTML markup and Markdown data that you seem to be working with are data embedded in strings, which prettyprinter does not pretty print other than as generic strings. Feel free to reopen if you provide more context as to what you are trying to achieve.