Python API

ASCII diagrams language parser and renderer. print() and diagram().

In addition, Diagram class may be used to access the low-level API.

adia.diagram(source, rstrip=True)

High level API to generate ASCII diagram.

This function is equivalent to:

Diagram(source).renders()
Parameters
  • source (str or file-like) – The ADia source code.

  • rstrip (bool, optional, default: True) – If True, the trailing wihtespaces at the end of each line will be removed.

Returns

ASCII diagram.

Return type

str

adia.print(source, file=None, rstrip=True)

High level API to write ASCII diagram into file.

Equivalent to:

Diagram(source).render(file)

Example

 import adia

 source = '''
    diagram: Foo
    sequence:
    foo -> bar: Hello World!
''''
 with open('foo.txt', 'w') as outfile:
     adia.print(source, file=outfile)
Parameters
  • source (str or file-like) – The ADia source code.

  • file – An object with the write attribute. If None the sys.stdout will be used instead.

  • rstrip (bool, optional, default: True) – If True, the trailing wihtespaces at the end of each line will be removed.

class adia.Diagram(source=None, *args, **kwargs)

The main entrypoint of the adia package.

Diagram is a collection of actual diagrams such as SequenceDiagram which implements the Interpreter abstract class and uses Tokenizer and Renderer internally to do it’s job.

Parameters

source (str or file-like) – ADia source code to parse.

Note

You may use the dumps() method to dump back the diagram instance to ADia source code.

dumps()

Serialize back the diagram class into valid ADia source code.

diagram = Diagram('''
    diagram: Foo
    sequence:
    foo -> bar: Hello World!
''')

print(diagram.dumps())
diagram: Foo

sequence:

foo -> bar: Hello World!
parse(source)

Parses Adia source string.

Parameters

source (str) – The ADia source code.

parsefile(sourcefile)

Parses an ADia source file into the current instance.

Parameters

sourcefile (file-like object) – The ADia source file.

parseline(line)

Parse an ADia source line into the current instance.

Parameters

line (str) –

render(outfile, rstrip=True)

Writes the ASCII represetation of the current instance into the outfile.

Parameters
  • outfile – An object with write(...) method.

  • rstrip (bool, optional, default: True) – If True, the trailing wihtespaces at the end of each line will be removed.

renders(rstrip=True)

Gets the ASCII represetation of the current instance.

Parameters

rstrip (bool, optional, default: True) – If True, the trailing wihtespaces at the end of each line will be removed.

Returns

ASCII diagram.

Return type

str

class adia.SequenceDiagram(*args, **kwargs)

Represents a sequence diagram.

The adia.diagram class creates an instance of this class for each sequence diagram section.