🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Using the textwrap micropython module?

    Micropython
    1
    2
    3.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • greenleafG
      greenleaf
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • greenleafG
        greenleaf
        last edited by greenleaf

        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'
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post