Prototyping/Download Sample Cut-up Share: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 6: Line 6:




== Inscape Effects: Hello World ==
Hello World, does nothing (yet)...
helloworld.py
<source lang="python">
import sys
from lxml import etree
# Read the XML from stdin
tree = etree.parse(sys.stdin)
# Manipulate the tree
#
# Output the etree to stdout
sys.stdout.write(etree.tostring(doc))
</source>
helloworld.inx
<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<_name>Hello World</_name>
<id>pzi.helloworld</id>
<dependency type="executable" location="extensions">helloworld.py</dependency>
  <_param name="title" type="description">This is a sample extension.</_param>
<param name="num" type="int" min="1" max="1000" _gui-text="Number of paragraphs">5</param>
<param name="sentencecount" type="int" min="2" max="100" _gui-text="Sentences per paragraph">16</param>
<param name="fluctuation" type="int" min="1" max="100" _gui-text="Paragraph length fluctuation (sentences)">4</param>
    <effect>
        <object-type>all</object-type>
        <effects-menu>
            <submenu _name="Python"/>
        </effects-menu>
    </effect>
    <script>
    <command reldir="extensions" interpreter="python">helloworld.py</command>
    </script>
</inkscape-extension>
</source>
== ==


Tools that will be useful: [[Python]], [[lxml.etree]], [[xpath]]
Tools that will be useful: [[Python]], [[lxml.etree]], [[xpath]]

Revision as of 12:35, 26 October 2011

Challenge: create an Inkscape extension to directly import images from the openclipart site.

From the Inkscape wiki:

(interpreter)? your_script (--param=value)* /path/to/input/SVGfile | inkscape


Inscape Effects: Hello World

Hello World, does nothing (yet)...

helloworld.py

import sys
from lxml import etree

# Read the XML from stdin
tree = etree.parse(sys.stdin)

# Manipulate the tree
#

# Output the etree to stdout
sys.stdout.write(etree.tostring(doc))

helloworld.inx

<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
	<_name>Hello World</_name>
	<id>pzi.helloworld</id>
	<dependency type="executable" location="extensions">helloworld.py</dependency>
  <_param name="title" type="description">This is a sample extension.</_param>
	<param name="num" type="int" min="1" max="1000" _gui-text="Number of paragraphs">5</param>
	<param name="sentencecount" type="int" min="2" max="100" _gui-text="Sentences per paragraph">16</param>
	<param name="fluctuation" type="int" min="1" max="100" _gui-text="Paragraph length fluctuation (sentences)">4</param>
    <effect>
        <object-type>all</object-type>
        <effects-menu>
            <submenu _name="Python"/>
        </effects-menu>
    </effect>
    <script>
    	<command reldir="extensions" interpreter="python">helloworld.py</command>
    </script>
</inkscape-extension>

Tools that will be useful: Python, lxml.etree, xpath

http://www.openclipart.org/docs/api

Get some feeds. NB wget's O option (and that's a CAPITAL O), allows to save to a reasonable filename of your choice.

wget http://www.openclipart.org/media/feed/rss/woman -O woman.xml
wget http://www.openclipart.org/media/feed/rss/man -O man.xml


Creating a simple HTML page from the feed

import codecs, sys, lxml.etree, urllib2

# Open Live URL
f = urllib2.urlopen("http://www.openclipart.org/media/feed/rss/woman")

# Open the filename given on the command line
# f = codecs.open(sys.argv[1], encoding="utf-8")

# Read in the XML file
doc = lxml.etree.parse(f)

# This is a Python dictionary containing
# the xml "namespaces" that we may use
NS = {
    'media': 'http://search.yahoo.com/mrss/',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'cc': 'http://creativecommons.org/ns#',
    'atom': 'http://www.w3.org/2005/Atom',
}

# Loop over the item elements
for item in doc.xpath("//item"):
    # NB: the "." at the start of the xpaths
    # makes the query relative to the current (context)
    # ie the particular item (and not the whole document)

    svg = item.xpath(".//enclosure/@url")[0]
    thumbnail_url = item.xpath(".//media:thumbnail/@url", namespaces=NS)[0]
    creator = item.xpath(".//dc:creator/text()", namespaces=NS)[0]
    title = item.xpath(".//title/text()")[0]
    link = item.xpath(".//link/text()")[0]

    # Output some HTML
    print """<div>
<a href="{1}"><img src="{2}" />{0}</a>
</div>""".format(title, link, thumbnail_url)

Resources