Tagged: widget Toggle Comment Threads | Keyboard Shortcuts

  • hardikmehta 9:02 pm on August 13, 2009 Permalink | Reply
    Tags: , , forecast, , , , plasmoid, pydev, pykde4, pyqt4, Python, python-plasmoid, qt4, , widget   

    A weather plasmoid in python 

    Ever since I heard about plasma I was eager to find ways to write my own plasmoid. But for the development of binary plasmoids, it is necessary to compile at least a part of kde4, at least this is what I understood from the prerequisite listed on the official tutorial. This discouraged me a bit. Moreover, I think that installation of the binary gadgets also requires to either package them properly according to the distribution you want to distribute for or you need to provide the sources that requires the users to build them. This  in my opinion is really a huge overhead for small applications like desktop gadgets / widgets. Then I found that it is also possible to develop plasmoids with script languages like python, ruby and javascript.

    Even though, I didn’t know PyQT4 or PyKDE4 libraries at all, I decided to write my first plasmoid in python.  This is an exremely simple plasmoid, the core of which was put together in a few hours on a rainy weekend. I have to admit that being a java developer, my code is not pythonic enough.

    It can be downloaded from: kde-look.

    Here the source code: plasma_pyweather.

    wplasmoid_greytransThe layout is based on my conky setup described here. I have tried to keep it as compact as possible including most of the provided information, if not all.  I don’t want the weather information to cover most of the screen.  The initial version of the plasmoid was written using the QT4 widgets, but was looking ugly.

    The weather information is fetched from google weather api. The location and the unit are configurable. The api provides forecast only for the next 3 days, so the number of forecast days were not made configurable.  Some how for google weather api the locale and so the unit is connected to the language, therefor  I had to convert the units myself. To keep the display compact I rounded the temperatures to integers.

    I got to learn PyQT4 and PyKDE4 libraries while writing this. Especially PyQT4 is a very powerful gui library for python.  It is fun programming with it. I have used eclipse + pydev for developing. In the beginning I didn’t have the auto-completion for PyQT4 as well as PyKDE4 in this environment, because they are not installed as pure python source code files. I could fix this with following answer.

    The other interesting things, I learned was to use the qt-designer tool for creating gui. It is a nice WYSIWYG tool for designing complex guis. The configuration form is designed using this tool. The qt-designer creates a .ui file, which is an xml file. The .ui file can be converted to the python source code using the pyuic4 tool in the following way.

    pyuic4 -o configForm_ui.py ../ui/configForm.ui
    

    I also learned to use the .qrc file for resources like images. The .qrc file can be converted into python source code using  pyrcc4 tool

    pyrcc4 -o images_rc.py images.qrc
    

    The qrc file is also an xml file which contains the references to the images used. It can be created by just editing.

    <RCC>
        <qresource prefix="/images">
            <file alias="sunny.svgz">../images/sunny.svgz</file>
            <file alias="showers.svgz">../images/showers.svgz</file>
            <file alias="not-available.svgz">../images/not-available.svgz</file>
            <file alias="cloudy.svgz">../images/cloudy.svgz</file>
            <file alias="thunderstorms.svgz">../images/thunderstorms.svgz</file>
            <file alias="haze.svgz">../images/haze.svgz</file>
            <file alias="windy.svgz">../images/windy.svgz</file>
        </qresource>
    </RCC>
    

    I decided to write this weather plasmoid in python, because all the other weather plasmoids I found were binaries. I have also tried to keep the data and presentation layer separate, I hope I succeeded in that.  In fact plasma framework provides a very good way of doing that in terms of data engines. I would also like to use the data engines, may be with this or other plasmoid I may develop.

    The svg images for weather conditions are taken from kde-look. The credit goes to the original artist, painkiller10. I have considered only a number of weather conditions, so If you see a question mark image as the weather condition, let me know which condition it is, so I can update the code to consider it.

    Please feel free to report any bugs or suggestions for improvement. I am sure there is a big room of improvement from code point of view as well as functionality. I am also open for changing the layout or changing the information displayed if someone comes with a better suggestion than the current one.

    Some more screen shots:

    Looks almost like conky with the seamless theme.

    Looks almost like conky with the seamless theme.

    wplasmoid_settings

    With settings dialog box

    Advertisements
     
    • Paolo 3:18 pm on September 2, 2009 Permalink | Reply

      Beautiful! BTW, could you internationalize your plasmoid? (in order to translate messages in other languages) Thanks.

      • hardikmehta 9:45 am on September 3, 2009 Permalink | Reply

        Thanks.

        I have plans to internationalize it, as soon as I get enough time to work on it.

    • arun kamath 5:04 am on September 13, 2009 Permalink | Reply

      I checked your source code at github. Instructions are for linux users. Can you please tell me how to run your program in windows?

    • Raffaele 2:06 pm on January 13, 2010 Permalink | Reply

      I like your plasmoid a lot, I’m enjoying it for months 😉
      Recently (maybe) google API changed a bit, because I got an error on wind string that halted my plasmoid 😦 Here is the patch
      http://pastebin.ca/1749545
      Where did you learn graphic widget programming for PyQt? I’d also like to, but I find the official doc inadequate (compared to Java’s for example… I’m a JStudent, too 🙂 )

      Cheers

      • hardikmehta 10:18 pm on January 13, 2010 Permalink | Reply

        Raffaele, thanks a lot. I have committed the patch to the repo and updated the package on kde-look. The incorrect format appears only for certain cities like Hong Kong. I tried to learn PyQT from the official documentation and also studying code of some existing plasmoids. It is true that compared to java, there is little help for learning PyQt or Python in general. I am a java developer just found python interesting. I am not sure about how pythonic my code is though 🙂

        Cheers.

    • DeltaKilo 4:10 pm on January 17, 2010 Permalink | Reply

      Good work! It is the first plasmoid showing weather in Russia!

      One little issue. SI unit for temperature isn’t °C but °K (Kelvin). It would be better to use imperial/metric switch.

    • DeltaKilo 4:11 pm on January 17, 2010 Permalink | Reply

      & kmph of course.. In SI should be mps

      • hardikmehta 12:05 pm on January 18, 2010 Permalink | Reply

        Thanks for using the plasmoid. Also thanks for pointing out the mistake. I corrected the names of the units and uploaded a new version with the new names. If you upgrade don’t for get to select the new units from the settings dialog box.

    • Raffaele 6:01 pm on April 8, 2010 Permalink | Reply

      Hi, I’m continuing enjoying your plasmoid 😀
      I found another little bug (actually a typo error) which sometimes prevents pyweather initialization. The problem occurs in weather.py http://pastebin.com/vX6HtkGb
      I don’t know if it’s fixed in most recent version, neither why sometimes it doesn’t fail. Happy coding!

      • hardikmehta 2:30 pm on April 20, 2010 Permalink | Reply

        Thanks for pointing out. I corrected and committed in the repo. Do you think we need a release for this ? I would rather wait and try to pack some feature with the bug fix.

    • Rick 5:16 pm on April 21, 2010 Permalink | Reply

      Hi, Thanks for the great plasmoid.
      Has everything my wife and I want except the ability to change font color. We noticed that the initial install will put in a dark or light font color (contrasting) according to the wallpaper used at that time. Would like to change the contrast when a different wallpaper is used. Is that possible?
      Thanks again.

      • hardikmehta 7:47 pm on April 21, 2010 Permalink | Reply

        Hi,

        Thanks for using the plasmoid.

        Actually, I just set the default font color for text which is selected by the current KDE theme. So, changing the font color with the current version of the plasmoid is only possible if you change the kde theme, which of course will change many other things. I liked your suggestion and would try to implement the font-color as config properties.

        cheers,
        Hardik

    • AmFreak 3:29 am on June 2, 2010 Permalink | Reply

      Hi,

      im planning to write a Plasmoid myself but i have a question.
      Is there a way to start the Plasmoid from Eclipse or do u have to run a script everytime u want to test it, like seen in the Getting Started Tutorial (http://techbase.kde.org/Development/Tutorials/Plasma/Python/GettingStarted) ?
      Thanks in advance.

      AmFreak

      • hardikmehta 6:43 am on June 2, 2010 Permalink | Reply

        I think pydev plugin provides ways to start any python script from eclipse, but I didn’t use that feature so cannot say how to do it. The recommended way to test is with plasmoidviewer, the application which comes as part of plasma environment.

    • AmFreak 2:34 am on June 5, 2010 Permalink | Reply

      But how can you debug a plasmoid if you only can test it with plasmoidviewer ?

    • luis 6:11 am on November 24, 2010 Permalink | Reply

      Thanks yu very much for the plasmoid, I found it really useful and very stylish.

      One question? is there a way to change the default Munich location? every time that I have to restart or crash, it automatically changes to the default

    • Giorgos Komninos (@giokom) 5:50 pm on March 18, 2012 Permalink | Reply

      I found a bug and it’s solution
      check my comment :
      http://opendesktop.org/content/show.php?content=110137&forumpage=3

      • hardikmehta 10:38 am on March 19, 2012 Permalink | Reply

        Hi Giorgos,

        Thanks for finding and fixing the issue. I will correct and release a new version as soon as I get some time.

        regards,
        Hardik

    • mike welder 1:12 am on September 3, 2012 Permalink | Reply

      as of recently I have not been able to start this “script initialization failed” is the message I get.it was working fine then I had a few updates and the above message started.It’s hard for me to tell which update did it as my computer stays on for weeks at a time with out logout or restart unless I have to so it’s hard for me to tell which update did it and when but it did start after a restart.I’m using fedora 17 kde.dam shame the one reason I’ve been with kde since fedora 15 is the great weather widget and a few others.Great work by the way.It’s not only your work there are a few others with the same message.

  • hardikmehta 7:35 am on August 4, 2009 Permalink | Reply
    Tags: bash, , , , , , kde, , , , plugin, , weather forecast, widget, xml, xslt, xsltproc   

    A script to display weather forecast for conky 

    Conky is undoubtedly the most configurable, versatile and amazing program I have ever come across.  With very little effort you can get a very sleek looking desktop. I have seen people using it to display all kinds of information under the sky.  Although, it can only display pure text, there is a possibility to use different symbolic fonts to display text based images.  Moreover, it is also very efficient program. Its own footprint is very small compared to most of the gadget frameworks like adesklets, gDesklets and of course plasma.  Configuration is also relatively easy. The best way is to copy configuration file from some one else and keep on changing it until you get what you want.  Another application worth mentioning here is dzen2. Dzen2 has ability to render any text graphically, although it lacks the built in variables of conky. In archlinux there is an application package called conky-cli which is a stripped down version of conky without X11 dependency. A combination of conky-cli and dzen2 is then used to make taskbars for minimalistic desktops like xmonad, wmii, dwm etc. Personally, I haven’t tried this combination yet.

    conky screen shot

    Ubuntu and Archlinux forums have dedicated threads with conky screen shots and config files. I came across many screen shots with weather information, forecast and small symbols with weather conditions. Most of them were using a perl script (for which I cannot find the link now) to fetch, parse and show the weather information from weather.com.  The weather symbols were ttf fonts.  I didn’t want to take the script one-to-one and perl is anyway too cryptic for me to  decipher. I decided to take the concept and develop something similar on my own.

    So I created the google weather script. It is a simple bash script to fetch the xml file using google weather api, I changed the source because although weather.com was providing good information, they were changing the format of the request too often, their terms and conditions of usage were also not very comprehensible for me. Then I use different style-sheets to process the xml response and show different information depending on the argument passed to the bash script. I also use the weather.ttf fonts to render the symbols for different weather conditions.  For transforming xml, I use the xsltproc tool.

    Here is the conky running under kde4 desktop. With different desktop environments       conky needs to be adjusted  a bit. Another weakness of conky in integrating external scripts is that the output must be pre-formatted before supplying it to conky. Conky just takes the text and renders it.  In principle any executable can be called from conky to display its output.

    Conky has many built-in variables for displaying common system parameters which are well documented on the official documentation page. Besides the common parameters like cpu / memory usage, uptime, upload-download speed, top 3 cpu using processes and top 3 memory using processes, the screen shot on the left also displays the dictionry.com word of the day rss feed, which I find extremely useful. It also uses a similar script and xslt transformation. There is also a built-in rss feed processor in conky.

    I use mpd for playing music. There is also built-in mpd support in conky and can be used to display information about current song being played. If you want to show different information at different places on the desktop, it is also possible to have multiple instances of conky running at the same time with different configuration files.

    As evident  in the screen shot it is difficult to adjust the spacing of the weather output due to mixing of different types and sizes of  fonts.  My script only considers some frequent weather conditions, but it can easily adapted to support many different weather conditions.  The spacing must be adjusted for individual setup by editing the line no. 16 in the  file fcConditions.xslt

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
        <xsl:include href="conditionsInclude.xslt"/>
        <xsl:output method="text" disable-output-escaping="yes" encoding="utf-8"/>
        <xsl:template match="xml_api_reply">
            <xsl:apply-templates select="weather"/>
        </xsl:template>
    
        <xsl:template match="weather">
            <xsl:for-each select="forecast_conditions&#91;position() >= 2]">
               <xsl:call-template name="get-condition-symbol">
                    <xsl:with-param name="condition">
                        <xsl:value-of select="condition/@data"/>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:if test="position() != 3">
                    <xsl:text> </xsl:text>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    I have hard-coded the update interval to 1 hour. This can also be changed very easily to the required value by changing the value of the update variable in the weather.sh file.

    # don't get the file if created within an hour
    update=3600
    

    I use mainly following configurations for conky.

    I have released the script with GPL V3 license, so feel free to use it if you like it. Please comment about any bug reports, improvements, questions or suggestions.

     
    • antibalas 4:40 am on October 30, 2009 Permalink | Reply

      great work.

    • foenOsteotNox 12:17 am on December 11, 2009 Permalink | Reply

      I’m often searching for recent blogposts in the WWW about this issue. Thx!

    • webrev 7:13 pm on August 6, 2010 Permalink | Reply

      Love the google weather script. Thanks. Can someone tell me how to change the temp units to standard. I have changed in weather.sh

      1. s=standard units, m=metric units

      UNITS=s

      But it still reports as metric.

      • hardikmehta 8:16 pm on August 6, 2010 Permalink | Reply

        Hi,

        Thanks for using the script. The UNITS variable is obsolete with the google api implementation. It worked with the weather.com version.

        With google apis, it is a bit different. The default locale of the url is “us” which is used in the script. In this case the temperatures are provided in both metric and standard units. See temp_c elements in the xml http://www.google.com/ig/api?weather=Munich,Germany

        If you want a localized output please append hl=[iso locale code] to the url, but then the output is also in the language specifed by the locale, so the script is not guranteed wo to work with it.

        example of German locale (de)
        http://www.google.com/ig/api?weather=Munich,Germany&hl=de

        I hope this helps. Feel free to ask if you have further questions.

    • Ben Brown 5:54 pm on February 23, 2011 Permalink | Reply

      Just wanted to let know how much I appreciate the work you did on the weather program for conky. I don’t understand all the programs and coding, but I am working my way through the code and have been able to get it to work for me here in Michigan, USA.

      Again, thank you so much for sharing this with the community.

      • hardikmehta 6:01 pm on February 23, 2011 Permalink | Reply

        Hi, Thanks. Glad that it worked for you.

    • Agnelo de la Crotche 2:26 pm on May 21, 2011 Permalink | Reply

      Great job! What about packaging it for different distros in OBS (https://build.opensuse.org) ?

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: