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

Expose save_png in save #3155

Merged
merged 1 commit into from
Jan 30, 2022
Merged

Expose save_png in save #3155

merged 1 commit into from
Jan 30, 2022

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Jan 30, 2022

Fixes #3005

save_png.mp4
Code
from io import BytesIO

import hvplot.pandas
import panel as pn
import pandas as pd
import param

pn.extension(sizing_mode="stretch_width")
ACCENT_COLOR = "#0072B5"


class Explorer(param.Parameterized):
    df = pd.DataFrame({"y": range(10)})

    def __init__(self, **params):
        super().__init__(**params)
        self.plot = pn.Column(
            self.df.hvplot().opts(color=ACCENT_COLOR, line_width=6, responsive=True),
            self.df.hvplot().opts(color=ACCENT_COLOR, line_width=6, responsive=True),
        )
        self.save_to_png = pn.widgets.FileDownload(
            filename="timeseries.png",
            label="Download .png",
            button_type="primary",
            callback=self.download_column,
        )

    def download_column(self):
        self.save_to_png.loading = True

        b = BytesIO()
        self.plot.save(b, as_png=True)
        b.seek(0)

        self.save_to_png.loading = False
        return b


explorer = Explorer()

pn.template.FastListTemplate(
    site="Awesome Panel",
    title="Application with .png download",
    sidebar=[explorer.save_to_png],
    main=[explorer.plot],
    accent_base_color=ACCENT_COLOR,
    header_background=ACCENT_COLOR,
).servable()

I don't know if this should also be made available for saving the template, right here:

def save(self, filename, title=None, resources=None, embed=False,
max_states=1000, max_opts=3, embed_json=False,
json_prefix='', save_path='./', load_path=None):

I have observed that sometimes the saved plot is cropped, but I don't think this is related to the changes in the PR.
Cropped
cropped

Not cropped
not cropped

@codecov
Copy link

codecov bot commented Jan 30, 2022

Codecov Report

Merging #3155 (a87c669) into master (e8746ef) will decrease coverage by 0.00%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3155      +/-   ##
==========================================
- Coverage   83.00%   83.00%   -0.01%     
==========================================
  Files         192      192              
  Lines       25477    25483       +6     
==========================================
+ Hits        21147    21151       +4     
- Misses       4330     4332       +2     
Impacted Files Coverage Δ
panel/viewable.py 70.44% <ø> (ø)
panel/io/save.py 62.60% <66.66%> (+0.32%) ⬆️
panel/io/state.py 68.83% <0.00%> (-0.25%) ⬇️
panel/widgets/base.py 94.73% <0.00%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e8746ef...a87c669. Read the comment docs.

@philippjfr philippjfr merged commit 39ea2f5 into holoviz:master Jan 30, 2022
@philippjfr
Copy link
Member

Thanks @hoxbro!

@hoxbro hoxbro deleted the save_png branch January 30, 2022 16:26
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

Successfully merging this pull request may close these issues.

Enable saving to BytesIO png.
2 participants