def encode(self, obj: NDFrame, description: Optional[str], params: Optional[Dict]) -> FrameData: buf = StringIO() obj.to_csv(buf, index=self.index, header=self.header, encoding=self.encoding, quoting=QUOTE_ALL) index_type = [str(obj.index.dtype)] if self.index else [] return FrameData(BytesContent(buf.getvalue().encode(self.encoding)), MediaType("text/csv", self.application()), description, params, {"header": self.header, "index": self.index, "schema": index_type + self.schema(obj), "encoding": self.encoding, "version": pandas_version})
def pull_data(context: Context, params: Optional[Dict] = None, **kwargs) -> FrameData: path = context.stack_path() params = merge_or_none(params, kwargs) res = context.protocol.pull(path, context.profile.token, params) attach = res["attachment"] data = \ BytesContent(base64.b64decode(attach["data"])) if "data" in attach else \ StreamContent(*context.protocol.download(attach["download_url"])) media_type = MediaType(attach["content_type"], attach.get("application", None)) return FrameData(data, media_type, attach.get("description", None), attach.get("params", None), attach.get("settings", None))
def encode(self, obj: Figure, description: Optional[str], params: Optional[Dict]) -> FrameData: """Convert Bokeh figure to frame data. Args: obj (bokeh.plotting.figure.Figure): Bokeh's figure to publish. description: Bokeh plot description. params: Bokeh plot parameters. Returns: Frame data. """ text = dumps(json_item(obj)) return FrameData(BytesContent(text.encode("utf-8")), MediaType("application/json", "bokeh"), description, params, {"bokeh_version": bokeh_version})
def encode(self, obj: Figure, description: Optional[str], params: Optional[Dict]) -> FrameData: """Convert matplotlib figure to frame data. Notes: Figure will be converted to SVG format. Args: obj: Plot to be published. description: Description of the plot. params: Plot parameters if specified. Returns: Corresponding `FrameData` object. """ buf = io.BytesIO() obj.savefig(buf, format="svg") return FrameData(BytesContent(buf), MediaType("image/svg+xml", "matplotlib"), description, params)
def pull_data(context: Context, params: ty.Optional[ty.Dict] = None, meta: ty.Optional[ty.Dict] = None, **kwargs) -> FrameData: path = context.stack_path() params = merge_or_none(params, kwargs) # TODO: Split context.protocol.pull into to pull_head and pull_frame frame, index, res = context.protocol.pull(path, context.profile.token, params, meta) attach = res["attachment"] data = _cache_attach_data(attach, context, frame, index, path) media_type = MediaType(attach["content_type"], attach.get("application", None)) return FrameData(data, media_type, attach.get("description", None), attach.get("params", None), attach.get("settings", None))
def encode(self, obj: BaseEstimator, description: Optional[str], params: Optional[Dict]) -> FrameData: buf = self.persistence.encode(obj) settings = { "class": f"{obj.__class__.__module__}.{obj.__class__.__name__}", "scikit-learn": sklearn.__version__, "scipy": scipy.__version__, "numpy": numpy.__version__, "joblib": joblib.__version__, "storage_format": self.persistence.storage() } if obj.__class__ in self.map: model_info = self.map[obj.__class__](obj) settings["info"] = model_info.settings() return FrameData(BytesContent(buf), self.persistence.type(), description, params, settings)
def encode(self, obj: Figure, description: Optional[str], params: Optional[Dict]) -> FrameData: """Build frame data object from Plotly figure. Args: obj (plotly.graph_objs._figure.Figure): A plotly figure to publish. description: Description of Plotly chart. params: Parameters of the chart. Returns: Frame data. """ json = obj.to_json() return FrameData( BytesContent(json.encode("utf-8")), MediaType("application/json", "plotly"), description, params, { "plotly_version": plotly_version, "plotly_js_version": self.plotly_js_version })
def encode(self, obj: Markdown, description: Optional[str], params: Optional[Dict]) -> FrameData: return FrameData(BytesContent(obj.text.encode("utf-8")), MediaType("text/markdown", "markdown"), description, params)