Skip to content

Commit

Permalink
Merge pull request #165 from SylvainCorlay/output_widget
Browse files Browse the repository at this point in the history
Add output widget
  • Loading branch information
SylvainCorlay committed Mar 25, 2019
2 parents e724139 + 5cc7c8b commit 2d463fb
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ set(XWIDGETS_HEADERS
${XWIDGETS_INCLUDE_DIR}/xwidgets/xnumber.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xnumeral.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xobject.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xoutput.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xpassword.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xplay.hpp
${XWIDGETS_INCLUDE_DIR}/xwidgets/xprogress.hpp
Expand Down Expand Up @@ -132,6 +133,7 @@ set(XWIDGETS_SOURCES
${XWIDGETS_SOURCE_DIR}/xlayout.cpp
${XWIDGETS_SOURCE_DIR}/xlink.cpp
${XWIDGETS_SOURCE_DIR}/xnumeral.cpp
${XWIDGETS_SOURCE_DIR}/xoutput.cpp
${XWIDGETS_SOURCE_DIR}/xpassword.cpp
${XWIDGETS_SOURCE_DIR}/xplay.cpp
${XWIDGETS_SOURCE_DIR}/xprogress.cpp
Expand Down
158 changes: 158 additions & 0 deletions include/xwidgets/xoutput.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/***************************************************************************
* Copyright (c) 2017, Sylvain Corlay and Johan Mabille *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XWIDGETS_OUTPUT_HPP
#define XWIDGETS_OUTPUT_HPP

#include <string>

#include "xholder.hpp"
#include "xmaterialize.hpp"
#include "xwidget.hpp"

namespace xw
{
/**********************
* output declaration *
**********************/

template <class O>
class output_guard;

template <class D>
class xoutput : public xwidget<D>
{
public:

using base_type = xwidget<D>;
using derived_type = D;

void serialize_state(xeus::xjson&, xeus::buffer_sequence&) const;
void apply_patch(const xeus::xjson&, const xeus::buffer_sequence&);

XPROPERTY(std::string, derived_type, msg_id);
XPROPERTY(std::vector<xeus::xjson>, derived_type, outputs);

void capture();
void release();
output_guard<D> guard();

protected:

xoutput();
using base_type::base_type;

private:

void set_defaults();
};

using output = xmaterialize<xoutput>;

using output_generator = xgenerator<xoutput>;

template <class O>
class output_guard
{
public:

explicit output_guard(const xoutput<O>& out);
~output_guard();

private:

xholder<xtransport> m_out;
};

/**************************
* xoutput implementation *
**************************/

template <class D>
inline void xoutput<D>::serialize_state(xeus::xjson& state, xeus::buffer_sequence& buffers) const
{
base_type::serialize_state(state, buffers);

set_patch_from_property(msg_id, state, buffers);
set_patch_from_property(outputs, state, buffers);
}

template <class D>
inline void xoutput<D>::apply_patch(const xeus::xjson& patch, const xeus::buffer_sequence& buffers)
{
base_type::apply_patch(patch, buffers);

set_property_from_patch(msg_id, patch, buffers);
set_property_from_patch(outputs, patch, buffers);
}

template <class D>
inline xoutput<D>::xoutput()
: base_type()
{
set_defaults();
}

template <class D>
inline void xoutput<D>::set_defaults()
{
this->_model_name() = "OutputModel";
this->_view_name() = "OutputView";
this->_view_module() = "@jupyter-widgets/output";
this->_model_module() = "@jupyter-widgets/output";
}

template <class D>
inline void xoutput<D>::capture()
{
const auto& parent_header = xeus::get_interpreter().parent_header();
if (parent_header.find("msg_id") != parent_header.end())
{
msg_id = parent_header["msg_id"].get<std::string>();
}
}

template <class D>
inline void xoutput<D>::release()
{
msg_id = "";
}

template <class D>
inline output_guard<D> xoutput<D>::guard()
{
return output_guard<D>(*this);
}

template <class O>
inline output_guard<O>::output_guard(const xoutput<O>& out)
: m_out(make_id_holder<xtransport>(out.id()))
{
m_out.template get<O>().capture();
}

template <class O>
inline output_guard<O>::~output_guard()
{
m_out.template get<O>().release();
}

/*********************
* precompiled types *
*********************/

#ifndef _WIN32
extern template class xmaterialize<xoutput>;
extern template xmaterialize<xoutput>::xmaterialize();
extern template class xtransport<xmaterialize<xoutput>>;
extern template class xgenerator<xoutput>;
extern template xgenerator<xoutput>::xgenerator();
extern template class xtransport<xgenerator<xoutput>>;
#endif
}
#endif
79 changes: 78 additions & 1 deletion notebooks/xwidgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1784,13 +1784,90 @@
"auto au = xw::audio_from_file(\"Big.Buck.Bunny.mp3\").finalize();\n",
"au"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include \"xwidgets/xoutput.hpp\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xw::output out;\n",
"out"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"{\n",
" // Using a scope guard to enable output capture\n",
" auto g = out.guard();\n",
" std::cout << \"This output is captured.\" << std::endl;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <xcpp/xdisplay.hpp>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"{\n",
" // Using a scope guard to clear output widget\n",
" auto g = out.guard();\n",
" xcpp::clear_output();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++14",
"language": "C++14",
"name": "xeus-cling-cpp14"
"name": "xcpp14"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
Expand Down
11 changes: 11 additions & 0 deletions src/xoutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "xwidgets/xoutput.hpp"

namespace xw
{
template class XWIDGETS_API xmaterialize<xoutput>;
template xmaterialize<xoutput>::xmaterialize();
template class XWIDGETS_API xtransport<xmaterialize<xoutput>>;
template class XWIDGETS_API xgenerator<xoutput>;
template xgenerator<xoutput>::xgenerator();
template class XWIDGETS_API xtransport<xgenerator<xoutput>>;
}

0 comments on commit 2d463fb

Please sign in to comment.