Skip to content

Commit

Permalink
Merge pull request #415 from palewire/patch-13
Browse files Browse the repository at this point in the history
Switch from iPython HTML to IFrame
  • Loading branch information
chekos authored Feb 1, 2024
2 parents 43a7dc9 + 13c7829 commit bcf8036
Showing 1 changed file with 15 additions and 13 deletions.
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

0 comments on commit bcf8036

Please sign in to comment.