Skip to content

[svg] Add rcParam["svg.id"] to add a top-level id attribute to <svg> #28536

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

Merged
merged 3 commits into from
Jul 12, 2024

Conversation

willemsk
Copy link
Contributor

PR summary

Exported SVGs now contain a fixed id="svg1" attribute in the top level <svg> tag.

<svg 
    xmlns:xlink="http://www.w3.org/1999/xlink"
    width="50pt" height="50pt"
    viewBox="0 0 50 50"
    xmlns="http://www.w3.org/2000/svg"
    version="1.1"
    id="svg1"
>

This is useful if you would like to link the entire matplotlib SVG file within another SVG file with the <use> tag.

<use
    width="50" height="50"
    xlink:href="mpl.svg#svg1" id="use1"
    x="0" y="0"
/>

Where the #svg1 indicator will now refer to the top level <svg> tag, and will hence result in the inclusion of the entire file.

Closes issue #28535

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@tacaswell tacaswell added this to the v3.10.0 milestone Jul 11, 2024
@tacaswell
Copy link
Member

Thank you for this work. I agree that this is useful and worth doing, but have some questions about the implementation.

I am a bit concerned about using a hard-coded value, would in be better to allow this to be threaded through fig.savefig(...) with a default of 'svg1'? Would a less generic string (like matplotlib or mpl) be better as a default?

@timhoffm
Copy link
Member

timhoffm commented Jul 11, 2024

I share the concern of a hard-coded value.

Configuration can be

  • either via savefig() - T.b.d. how to fit this in the signature. A top-level id parameter is a bit awkward, because it would only be used for the SVG renderer. Alternatives would be a generic rederer_params dict, or (a bit hacky) put it in metadata and let the SVG renderer pop it from there.
  • or if threading through safefig is too much song and dance API-wise, just create an rcParam. This would be a little more cumbersome to use but simpler to implement. - Ok if setting id is really rare.

I also believe, the default should be no attribute, as it is now. Setting the attribute should be opt-in.

@willemsk
Copy link
Contributor Author

Fair enough, I went the hardcoded route because there was indeed no straightforward path to pass it through plt.savefig().
I think my use-case is fairly niche, and it should become irrelevant when SVG2.0 is adopted (which may be never though).

If you agree on an approach I'd be happy to draft an implementation!

@timhoffm
Copy link
Member

timhoffm commented Jul 11, 2024

If the application is fairly niche, I'd go with an rcParam svg.id (default: None); see here:

image

@tacaswell
Copy link
Member

I also support going the rcparam route with default behavior being None as Tim proposes.

If not None, the `svg.id` rcParam value will be used for setting
the `id` attribute of the top `<svg>` tag.
@willemsk willemsk force-pushed the feature/svg-top-level-id branch from 3a66481 to a46dfdd Compare July 11, 2024 21:17
@willemsk
Copy link
Contributor Author

I've added an implementation using rcparams.

``svg.id`` rcParam
~~~~~~~~~~~~~~~~~~
The value of this new rcParam controls whether the top-level ``<svg>`` tag
contains an ``id`` attribute and what its value is. When set to ``None`` (the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
contains an ``id`` attribute and what its value is. When set to ``None`` (the
contains an :rc:`svg.id` attribute and what its value is. When set to ``None`` (the

to use our custom role.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Here, id is the attibute ins <svg>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty of rephrasing to make it more concise.

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conditional on the docs change to use the rc role goes in

@timhoffm timhoffm changed the title [svg] Add 'id="svg1"' attribute top svg tag [svg] Add rcParam["svg.id"] to add a top-level id attribute to <svg> Jul 12, 2024
@timhoffm timhoffm merged commit c56027b into matplotlib:main Jul 12, 2024
42 of 43 checks passed
@timhoffm
Copy link
Member

Thanks @willemsk and congratulations on your first contribution to Matplotlib! We'd welcome to see you back.

@willemsk willemsk deleted the feature/svg-top-level-id branch July 12, 2024 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants