Page 1 of 1

Multiline text in control pages - Solution!

PostPosted: Tue Sep 19, 2017 9:00 am
by aderrington
Hi,
So i've been wondering how to take a string (from Weather underground - Detailed forecast) and split it onto 2 lines for use in a control page.

i've had a friend work on something this morning that works really well, so thought i'd share it with you all.
I've created 2 variables that the control page reads the data from.
And also set up a trigger that performs this Python script whenever change is detected on the detailed forecast state.

The devices ID and Variable IDs will need to be changed to your specific IDs.
Also the character limit would also need to change depending on text size and label size.

Hope it's of some use to you guys!

Andrew

Code: Select all
weatherstring = indigo.devices[266180261].states["foreText1"]
CharacterLimit = 73
def splitlines(weatherstring):
    lines= []
    remainingtext = str(weatherstring)
    def sliceofffirstline(remainingtext):
        chunk = str(remainingtext[:CharacterLimit])
        lastspaceindex = chunk.rfind(' ')
        line = str(remainingtext[:lastspaceindex])
        remainingtext = remainingtext[lastspaceindex:]
        return remainingtext, line
   
    while True :
        if len(remainingtext) > CharacterLimit:
            remainingtext, slice = sliceofffirstline(remainingtext)
            lines.append(str(slice).strip())
        if len(remainingtext) <= CharacterLimit:
            remainingtext.strip()
            lines.append(str(remainingtext).
            strip())
            return lines
            break

lines = splitlines(weatherstring)
if len(lines)==1:
   indigo.variable.updateValue(1812552637,lines[0])
   indigo.variable.updateValue(753872597,"")
else:
   indigo.variable.updateValue(1812552637,lines[0])
   indigo.variable.updateValue(753872597,lines[1])

Re: Multiline text in control pages - Solution!

PostPosted: Tue Sep 19, 2017 11:15 am
by Different Computers
Not only does this take care of a wish of mine for a year or more, it's specifically using exactly the plugin I need to make the fix work in! Thanks very much.

Re: Multiline text in control pages - Solution!

PostPosted: Wed Sep 20, 2017 4:34 am
by DaveL17
There are a couple other options for you to consider for multiline text.

The matplotlib plugin has a built-in device for plotting multiline text. It converts the text to an image that you can then link to in a control page. One way I use this device type is for plotting severe weather alerts that can sometimes be quite long.

Here is another thread which discusses other options. There are several options in the thread and you should read to the end as some of the scripts evolved over time.

Lastly, on my GitHub Homepage is a link to a script that generates animated GIFs for multiline text (displaying one line of text at a time) that's kind of visually interesting. I wouldn't recommend this one for long blocks of text but it would be workable for something like 3-5 lines.

My GitHub Homepage also has links to some other scripts that you might find helpful, too.