Skip to content

Commit

Permalink
Add JUPYTER_WIDGETS_ECHO env variable for echo_update
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Jan 27, 2023
1 parent 282af18 commit af91126
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
19 changes: 18 additions & 1 deletion include/xwidgets/xcommon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
#include <utility>
#include <vector>

#include <xeus/xcomm.hpp>
#include <xtl/xoptional.hpp>

#include "xbinary.hpp"
#include "xeus/xcomm.hpp"
#include "xwidgets_config.hpp"

namespace xw
Expand Down Expand Up @@ -85,6 +87,13 @@ namespace xw

private:

/**
* Indicate whether a global setting activates or deactivates ``echo_update`` messages.
*
* If the optional is empty, then no setting is set explicitly.
*/
static xtl::xoptional<bool> global_echo_update();

bool
same_patch(const std::string&, const nl::json&, const xeus::buffer_sequence&, const nl::json&, const xeus::buffer_sequence&)
const;
Expand Down Expand Up @@ -132,6 +141,14 @@ namespace xw
auto const it = hold_state.find(name);
if ((it != hold_state.end()) && same_patch(name, *it, hold_buffers, state[name], buffers))
{
// If the "echo_update" is explicitly deactivated we do not send the message
auto const echo_update = global_echo_update();
if (echo_update.has_value() && !echo_update.value())
{
return;
}
// If the "echo_update" is explicitly activated or unspecified, we continue and
// send message
method = "echo_update";
}
// On the contrary, the update could differ from the change in the frontend.
Expand Down
66 changes: 66 additions & 0 deletions src/xcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
#include "xwidgets/xcommon.hpp"

#include <algorithm>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>

#include <xtl/xoptional.hpp>

#include "xeus/xinterpreter.hpp"
#include "xtarget.hpp"

Expand Down Expand Up @@ -194,6 +197,69 @@ namespace xw
m_comm.close(nl::json::object(), nl::json::object(), xeus::buffer_sequence());
}

namespace
{
std::string tolower(std::string s)
{
auto safe_tolower = [](unsigned char c)
{
return std::tolower(c);
};
std::transform(s.begin(), s.end(), s.begin(), safe_tolower);
return s;
}

std::string ltrim(std::string s)
{
auto const safe_isnotspace = [](unsigned char ch)
{
return !std::isspace(ch);
};
s.erase(s.begin(), std::find_if(s.begin(), s.end(), safe_isnotspace));
return s;
}

std::string rtrim(std::string s)
{
auto const safe_isnotspace = [](unsigned char ch)
{
return !std::isspace(ch);
};
s.erase(std::find_if(s.rbegin(), s.rend(), safe_isnotspace).base(), s.end());
return s;
}

std::string trim(std::string s)
{
rtrim(s);
ltrim(s);
return s;
}

bool is_true_string(const char* str)
{
const std::string s = tolower(trim(str));
static const auto trues = {"true", "on", "yes", "1"};
return std::find(trues.begin(), trues.end(), s) < trues.end();
}

xtl::xoptional<bool> get_tristate_env(const char* name)
{
const char* const val = std::getenv(name);
if (val == nullptr)
{
return {};
}
return is_true_string(val);
}
}

xtl::xoptional<bool> xcommon::global_echo_update()
{
static const auto out = get_tristate_env("JUPYTER_WIDGETS_ECHO");
return out;
}

bool
xcommon::same_patch(const std::string& name, const nl::json& j1, const xeus::buffer_sequence&, const nl::json& j2, const xeus::buffer_sequence&)
const
Expand Down

0 comments on commit af91126

Please sign in to comment.