Using the textwrap micropython module?



  • Has anyone tried this?

    https://github.com/micropython/micropython-lib/blob/master/textwrap/textwrap.py

    I managed to upload textwrap.py to my device and it seems to import fine. But whenever I attempt to use it I get an error. Here is the code I'm trying to work with:

    ...
        req = urequests.request(method='GET', url='https://icanhazdadjoke.com/', headers={'Accept':'application/json'})
        dad_json = json.loads((req.text))
        DadJoke = dad_json['joke']
        wrapped = textwrap.fill(str(DadJoke),width=30)
        dad_joke.set_text_color(0x33ff33)
        dad_joke.set_text(wrapped)
    

    If I simply set_text(str(DadJoke)) it works but it writes right off the edge of the screen.



  • I'd be open to any other methods you have for wrapping text. Or even generic instructions for importing external micropython modules...

    This works fine in my local python interpreter:

    from textwrap import *
    joke = "This is a really long joke. This is a really long joke.This is a really long joke.This is a really long joke.This is a really long joke."
    wrapper = textwrap.TextWrapper(width=30)
    print(wrapper.fill(joke))
    

    But when I try to run it on the m5stack device I get:

    'module' object has no attribute 'TextWrapper'