Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add format or automatic format parsing to dot.render() #44

Closed
geyang opened this issue Aug 16, 2017 · 1 comment
Closed

Add format or automatic format parsing to dot.render() #44

geyang opened this issue Aug 16, 2017 · 1 comment

Comments

@geyang
Copy link

geyang commented Aug 16, 2017

Hi @xflr6 ,

Thanks for this library!

I would like to request adding either

  1. a new attribute format to the dot.render() function

or / and

  1. make the dot.render function automatically parse the file format from the render path.

I can submit a pull request if needed, just want to discuss this first.

The pain-point at the moment is that to save the graph, I need to either

g = Graph(format="svg")
... # Losts of code

g.render(path="figures/this_particular_figure")

or do

g.format = "svg"
g.render(path="....")

and none of these is very appealing.

Thanks!
Ge

@xflr6
Copy link
Owner

xflr6 commented Aug 17, 2017

Thanks, glad if it is somewhat useful.
Not completely sure this is about the right thing though, as the .render()-method has no path argument:

>>> from graphviz import Graph
>>> g = Graph(format="svg")
>>> g.node('spam')
>>> g.render(path='spam')
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    g.render(path='spam')
TypeError: render() got an unexpected keyword argument 'path'

The method does have filename and directory arguments. However, the former is for the name of the source (DOT, text) file. The actual rendering then does dot -Tsvg -O Graph.gv which automatically appends the right file extension to the name (in this case, the rendered file would be Graph.gv.svg):

 -O          - Automatically generate an output filename based on the input file
name with a .'format' appended. (Causes all -ofile options to be ignored.)

In other words, this follows the idea from the Graphviz commands to specify the format to infer the file extension from instead of the other way around (which AFAIU is what you want to propose, right?).
Note that this is why the .render()-method returns the path to the rendered file.

Note that by default filename is inferred from name, so maybe you want to try:

>>> import graphviz
>>> g = graphviz.Graph('fig1', directory='figures', format='svg')
>>> g.node('spam')
>>> g.render()
'figures/fig1.gv.svg'

@xflr6 xflr6 closed this as completed Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants