Unicode support for node labels

454 views
Skip to first unread message

nernst

unread,
Jul 9, 2009, 2:17:44 PM7/9/09
to pygraphviz-discuss
Has anyone been able to use utf-8 characters, e.g. ✓, with PyGraphviz?
I would like to pass it in as part of my node label, but I get the
following error:
method __setitem__ in agraph.py at line 1586
raise "Attribute value must be a string"

Is it as simple as changing the string types to unicode (u'')?

I've tried the label with pure graphviz, and it doesn't complain.

Seems like a useful feature for extended charset users.

thanks
neil

Aric Hagberg

unread,
Jul 9, 2009, 3:43:18 PM7/9/09
to pygraphvi...@googlegroups.com

PyGraphviz tries to encode string data into UTF-8 since that is what
Graphviz handles. So e.g. the following will work:

In [27]: import pygraphviz

In [28]: G=pygraphviz.AGraph()

In [29]: n=u"\u2713"

In [30]: G.add_node(n)

In [31]: G.nodes()
Out[31]: ['\xe2\x9c\x93']

In [32]: G.write()
strict graph {
✓;
}

If you find some places (attributes?) where this isn't handled
correctly please let us know.


Aric

nernst

unread,
Jul 9, 2009, 4:08:33 PM7/9/09
to pygraphviz-discuss
Some experimenting:
if I find the node, then do
n.attr['label'] = 'hi' + u'\u2713' #checkmark
I get
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/pygraphviz-0.99.1-py2.6-macosx-10.3-i386.egg/
pygraphviz/agraph.py", line 1586, in __setitem__
raise "Attribute value must be a string"
TypeError: exceptions must be classes or instances, not str

(which is two errors :-)

making them both unicode doesn't work either.

However, specifying the symbol directly:
n.attr['label'] = '✓'

works fine.

On Jul 9, 3:43 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:

Aric Hagberg

unread,
Jul 9, 2009, 5:05:32 PM7/9/09
to pygraphvi...@googlegroups.com
On Thu, Jul 9, 2009 at 2:08 PM, nernst<neil....@gmail.com> wrote:
>
> Some experimenting:
> if I find the node, then do
>  n.attr['label'] = 'hi' + u'\u2713' #checkmark
> I get
>  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/site-packages/pygraphviz-0.99.1-py2.6-macosx-10.3-i386.egg/
> pygraphviz/agraph.py", line 1586, in __setitem__
>    raise "Attribute value must be a string"
> TypeError: exceptions must be classes or instances, not str
>
> (which is two errors :-)
>
> making them both unicode doesn't work either.
>
> However, specifying the symbol directly:
>  n.attr['label'] = '✓'
>
> works fine.

Well, we had previously fixed one of those at least (raising the exception).

There are a few other places (in addition to the one you found)
where we are not encoding to UTF-8 before we attempt to store
data into the Graphviz data structure. I'll see if I can figure out a clean
way to handle that.

Aric

Aric Hagberg

unread,
Jul 9, 2009, 5:54:54 PM7/9/09
to pygraphvi...@googlegroups.com
On Thu, Jul 9, 2009 at 3:05 PM, Aric Hagberg<aric.h...@gmail.com> wrote:
> On Thu, Jul 9, 2009 at 2:08 PM, nernst<neil....@gmail.com> wrote:
>>
>> Some experimenting:
>> if I find the node, then do
>>  n.attr['label'] = 'hi' + u'\u2713' #checkmark
>> I get
>>  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
>> python2.6/site-packages/pygraphviz-0.99.1-py2.6-macosx-10.3-i386.egg/
>> pygraphviz/agraph.py", line 1586, in __setitem__
>>    raise "Attribute value must be a string"
>> TypeError: exceptions must be classes or instances, not str
>>
>> (which is two errors :-)
>>
>> making them both unicode doesn't work either.
>>
>> However, specifying the symbol directly:
>>  n.attr['label'] = '✓'
>>

Do you understand the Graphviz text encoding enough to give advice
on how to implement this? I get confused every time I look at
http://www.graphviz.org/doc/FAQ.html#Q10
and
http://www.graphviz.org/doc/info/lang.html

Normally I would probably want to store string-like data as unicode
and then encode to whatever the user specifies on output. But we are
storing string data in the internal Graphviz data structures
(c.f. libcgraph) and need to encode to whatever that requires.

Aric

nernst

unread,
Jul 17, 2009, 2:28:45 PM7/17/09
to pygraphviz-discuss
Yeah, that looks pretty ugly. Sorry, I have a hard time just getting
it to work in Python.

On Jul 9, 5:54 pm, Aric Hagberg <aric.hagb...@gmail.com> wrote:
> On Thu, Jul 9, 2009 at 3:05 PM, Aric Hagberg<aric.hagb...@gmail.com> wrote:
> > On Thu, Jul 9, 2009 at 2:08 PM, nernst<neil.er...@gmail.com> wrote:
>
> >> Some experimenting:
> >> if I find the node, then do
> >>  n.attr['label'] = 'hi' + u'\u2713' #checkmark
> >> I get
> >>  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> >> python2.6/site-packages/pygraphviz-0.99.1-py2.6-macosx-10.3-i386.egg/
> >> pygraphviz/agraph.py", line 1586, in __setitem__
> >>    raise "Attribute value must be a string"
> >> TypeError: exceptions must be classes or instances, not str
>
> >> (which is two errors :-)
>
> >> making them both unicode doesn't work either.
>
> >> However, specifying the symbol directly:
> >>  n.attr['label'] = '✓'
>
> Do you understand the Graphviz text encoding enough to give advice
> on how to implement this?  I get confused every time I look athttp://www.graphviz.org/doc/FAQ.html#Q10
> andhttp://www.graphviz.org/doc/info/lang.html
Reply all
Reply to author
Forward
0 new messages