Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • kmantu 3 posts 20 karma points
    Jan 23, 2009 @ 00:56
    kmantu
    0

    Access template properties and value via Python

    Hi everyone,

    New to umbraco and like it alot.

    I haven't seen much documentation on using python scripts with Umbraco.

    I'm particularly interested in how to access the passed in data such as macro parameter values and other user defined data.

    I've seen the XSLT approach using /macro/parametername but not sure how to translate that into python.

    I've also successfully used some of the Umbraco API from python but don't have a clue on accessing the userinput defined values for a particular macroproperty.

    Thanks in advance for any help with this.

    Kevin

  • Immo Wache 69 posts 224 karma points
    Jan 23, 2009 @ 16:21
    Immo Wache
    0

    Hi Kevin,

    welcome to Umbraco. Yes, you are right, the Python thing in Umbraco is almost like a top secret area. I assume because of the developer is not part of the core team.

    Some month ago I did some google searches for more informations. Finally I used the force and red the source :-)

    And with some success. I wrote some example Python macros and it works well. One of it is a Macro to remote wake up my new server at home by calling a special Web address of the site of my provider. This works fine for month without any care, especially it's funny to start the PC from my iPhone by calling the special web site.

    OK, but back to your question.
    After success with the Python macros I deceided to start some small Python articles on my blog. Unfortunately they are in german language. But to your luck, the first article is translated into english by Regina, a fellow girl and she is english trainer and translater.
    You can find it at:
    http://www.ucc-bln.de/weblog-english.aspx
    It's a starting point for informations.

    Then you can find the german Python articles in the main part of my blog.
    http://www.ucc-bln.de

    Simply search for python on the right side. I expect you can get these sites by google translate in englisch (may be a little ugly but I hope you can understand what google think it's meant to be).

    I which you much success with starting Python on Umbraco, Immo

  • kmantu 3 posts 20 karma points
    Jan 23, 2009 @ 19:13
    kmantu
    0

    Immo,

    Thanks for your response. Believe it or not, I read your blog before I started this thread but, unfortunately, I am still not able to find out how to access the incoming parameters via python.

    Your article did help with understanding how to access the Umbraco API. I was able to use python to get the list of Macros in the system and the name of their MacroParameters. However, that is static information.

    What I am trying to do with Python is already explained using XSLT, which is, grab the incoming user parameter data that was defined for that page. For example, let's say I created a python macro called "Birthday" with a parameter "Toy" and on one template set that parameter to "rubberduckie" and on another I set it to "speedboat." What is the python syntax for retrieving the value "rubberduckie" or "speedboat."

    Using the Umbraco API I can access the Macro "Brithday" and get the basic information about "Toy." But I don't see a way to get at the template info in general and the user-defined Macro data in particular.

    Thanks again for you help.

    Kevin



  • Immo Wache 69 posts 224 karma points
    Jan 23, 2009 @ 19:52
    Immo Wache
    0

    Hi again,

    some time ago TimG wrote a nice mashup macro to include YouTube videos into
    umbraco by use of a XSLT macro.

    You can find detailed explaination at:
    Creating a youtube macro for umbraco (with xslt extension)

    So I tried to get the same with Python.
    And how you can see it is more elegant than using an additional C# function.
    In detail you can see how the Macro parameter goes into the Python script.
    HTH, Immo

    [code]import re # for regular expressions

    from System.Web import * # for HttpContext class
    session = HttpContext.Current.Session # get the current session
    args = session['args'] # get the macro parameters from session

    * * * * * * * * * * * * * The fun starts here * * * * * * * * * * * * * * * * * * *


    url = args['youTubeUrl'] # get macro parameter youTubeUrl

    # and the rest is Python :-)

    m = re.match(r"(http://)?www.youtube.com/watch\?v=(\w[\w|-]*)", url)
    if m: # check if it matches
    videoId = m.group(2) # get the YouTube video id
    print '''
    ''' % locals() # insert the YouTube html object
    else:
    print "

    Error: This is not a YouTube link

    "
    exit # error -> check the macro
    # parameter value
    [/code]

  • kmantu 3 posts 20 karma points
    Jan 23, 2009 @ 21:51
    kmantu
    0

    That does it, Immo. Many thanks.

    I'm not much of a XSLT fan so I'm excited about using python and C# with Umbraco. I'll keep watching your blog for any other good stuff you may publish.

    Thanks again for helping me out.

    Kevin

Please Sign in or register to post replies

Write your reply to:

Draft