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

matplotlib svg #36

Open
Isaac-Flath opened this issue Aug 23, 2024 · 2 comments
Open

matplotlib svg #36

Isaac-Flath opened this issue Aug 23, 2024 · 2 comments

Comments

@Isaac-Flath
Copy link
Collaborator

Discord message: https://discord.com/channels/689892369998676007/1247700012952191049/1276592777823653979


Inspired by fh-matplotlib, I wrote a decorator for matplotlib svg, which gives better resolution as it uses vector graph.
Here's the code:

def fh_svg(func):
    "svg to fasthtml item"
    def wrapper(*args, **kwargs):
        func(*args, **kwargs)

        f = io.StringIO()
        plt.savefig(f, format='svg', bbox_inches='tight')

        f.seek(0)
        svg_data = f.getvalue()
        plt.close()

        return NotStr(svg_data)
    return wrapper

To use:

@fh_svg
def matplotlib_plot():
   plt.plot(np.arange(25), np.random.exponential(1, size=25))
   
# to display in fasthtml
Div(matplotlib_plot())    
@Isaac-Flath
Copy link
Collaborator Author

  • Create a full minimal app
  • Get img & gif
  • Metadata file

@sky1ove
Copy link
Contributor

sky1ove commented Aug 23, 2024

credit to Lily Cai

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