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

Switch from iPython HTML to IFrame #415

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions datawrapper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pandas as pd
import requests as r
from IPython.display import HTML, Image
from IPython.display import IFrame, Image

from .exceptions import FailedRequest, InvalidRequest

Expand Down Expand Up @@ -922,7 +922,7 @@ def delete_chart(self, chart_id: str) -> bool:
"""
return self.delete(f"{self._CHARTS_URL}/{chart_id}")

def display_chart(self, chart_id: str) -> HTML:
def display_chart(self, chart_id: str) -> IFrame:
"""Displays a datawrapper chart.

Parameters
Expand All @@ -932,12 +932,14 @@ def display_chart(self, chart_id: str) -> HTML:

Returns
-------
IPython.display.HTML
HTML displaying the chart.
IPython.display.IFrame
IFrame displaying the chart.
"""
obj = self.get_chart(chart_id)
iframe = obj["metadata"]["publish"]["embed-codes"]["embed-method-iframe"]
return HTML(iframe)
src = obj["publicUrl"]
width = obj["metadata"]["publish"]["embed-width"]
height = obj["metadata"]["publish"]["embed-height"]
return IFrame(src, width=width, height=height)

def copy_chart(self, chart_id: str) -> dict:
"""Copy one of your charts, tables, or maps and create a new editable copy.
Expand Down Expand Up @@ -988,7 +990,7 @@ def move_chart(self, chart_id: int, folder_id: int) -> dict:
data={"folderId": folder_id},
)

def publish_chart(self, chart_id: str, display: bool = False) -> dict | HTML:
def publish_chart(self, chart_id: str, display: bool = False) -> dict | IFrame:
"""Publishes a chart, table or map.

Parameters
Expand All @@ -1000,17 +1002,17 @@ def publish_chart(self, chart_id: str, display: bool = False) -> dict | HTML:

Returns
-------
dict | HTML
Either a dictionary containing the published chart's information or an HTML
dict | IFrame
Either a dictionary containing the published chart's information or an IFrame
object displaying the chart.
"""
obj = self.post(f"{self._CHARTS_URL}/{chart_id}/publish")
assert isinstance(obj, dict)
if display:
iframe = obj["data"]["metadata"]["publish"]["embed-codes"][
"embed-method-iframe"
]
return HTML(iframe)
src = obj["data"]["publicUrl"]
width = obj["data"]["metadata"]["publish"]["embed-width"]
height = obj["data"]["metadata"]["publish"]["embed-height"]
return IFrame(src, width=width, height=height)
else:
return obj

Expand Down
Loading