🤖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?

    Scheduled Pinned Locked Moved Micropython
    2 Posts 1 Posters 4.4k Views 1 Watching
    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 Offline
      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 Offline
        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

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • First post
          Last post