Skip to content

Commit

Permalink
Merge pull request #213 from SylvainCorlay/factor-mime_bundle_repr
Browse files Browse the repository at this point in the history
Precompile xcommon mime_bundle_repr
  • Loading branch information
SylvainCorlay committed Nov 10, 2020
2 parents 0d2a942 + 399b7d9 commit b20af60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/xwidgets/xcommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ namespace xw

void XWIDGETS_API to_json(nl::json& j, const xcommon& o);

/***********************************
* mime_bundle_repr specialization *
***********************************/

nl::json XWIDGETS_API mime_bundle_repr(const xcommon& val);

/********************************************
* xcommon template methods implementations *
********************************************/
Expand Down
14 changes: 2 additions & 12 deletions include/xwidgets/xmaterialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "nlohmann/json.hpp"

#include "xwidgets_config.hpp"
#include "xcommon.hpp"

namespace nl = nlohmann;

Expand Down Expand Up @@ -132,18 +133,7 @@ namespace xw
template <template <class> class B, class... P>
nl::json mime_bundle_repr(const xmaterialize<B, P...>& val)
{
nl::json mime_bundle;

// application/vnd.jupyter.widget-view+json
nl::json widgets_json;
widgets_json["version_major"] = XWIDGETS_PROTOCOL_VERSION_MAJOR;
widgets_json["version_minor"] = XWIDGETS_PROTOCOL_VERSION_MINOR;
widgets_json["model_id"] = val.id();
mime_bundle["application/vnd.jupyter.widget-view+json"] = std::move(widgets_json);

// text/plain
mime_bundle["text/plain"] = "A Jupyter widget";
return mime_bundle;
return mime_bundle_repr(static_cast<const xcommon&>(val));
}

/*************
Expand Down
16 changes: 16 additions & 0 deletions src/xcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,20 @@ namespace xw
{
j = "IPY_MODEL_" + std::string(o.id());
}

nl::json mime_bundle_repr(const xcommon& val)
{
nl::json mime_bundle;

// application/vnd.jupyter.widget-view+json
nl::json widgets_json;
widgets_json["version_major"] = XWIDGETS_PROTOCOL_VERSION_MAJOR;
widgets_json["version_minor"] = XWIDGETS_PROTOCOL_VERSION_MINOR;
widgets_json["model_id"] = val.id();
mime_bundle["application/vnd.jupyter.widget-view+json"] = std::move(widgets_json);

// text/plain
mime_bundle["text/plain"] = "A Jupyter widget with unique id: " + std::string(val.id());
return mime_bundle;
}
}

0 comments on commit b20af60

Please sign in to comment.