forked from jupyter-xeus/xeus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdisplay.cpp
1285 lines (1026 loc) · 38.2 KB
/
xdisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include "nlohmann/json.hpp"
#include "xeus/xinterpreter.hpp"
#include "pybind11_json/pybind11_json.hpp"
#include "pybind11/pybind11.h"
#include "pybind11/functional.h"
#include "pybind11/stl.h"
#include "xeus-python/xutils.hpp"
#include "xdisplay.hpp"
#include "xinternal_utils.hpp"
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif
namespace py = pybind11;
namespace nl = nlohmann;
using namespace pybind11::literals;
namespace xpyt_ipython
{
/****************************************
* xpublish_display_data implementation *
****************************************/
void xpublish_display_data(const py::object& data, const py::object& metadata, const py::object& transient, bool update)
{
auto& interp = xeus::get_interpreter();
// Make sure transient is not None
py::object transient_ = transient;
if (transient_.is_none())
{
transient_ = py::dict();
}
if (update)
{
interp.update_display_data(data, metadata, transient_);
}
else
{
interp.display_data(data, metadata, transient_);
}
}
/********************************************
* xpublish_execution_result implementation *
********************************************/
void xpublish_execution_result(const py::int_& execution_count, const py::object& data, const py::object& metadata)
{
auto& interp = xeus::get_interpreter();
nl::json cpp_data = data;
if (cpp_data.size() != 0)
{
interp.publish_execution_result(execution_count, std::move(cpp_data), metadata);
}
}
/*************************
* xclear implementation *
*************************/
void xclear(bool wait = false)
{
auto& interp = xeus::get_interpreter();
interp.clear_output(wait);
}
/******************
* display module *
******************/
py::module get_display_module_impl()
{
py::module display_module = xpyt::create_module("display");
display_module.def("publish_display_data",
xpublish_display_data,
"data"_a,
"metadata"_a=py::dict(),
"transient"_a=py::dict(),
"update"_a=false
);
display_module.def("publish_execution_result",
xpublish_execution_result,
"execution_count"_a,
"data"_a,
"metadata"_a=py::dict()
);
display_module.def("clear_output",
xclear,
"wait"_a=false
);
return display_module;
}
}
namespace xpyt_raw
{
bool safe_exists(const py::object& path)
{
py::module os = py::module::import("os");
try
{
return xpyt::is_pyobject_true(os.attr("path").attr("exists")(path));
}
catch (py::error_already_set& e)
{
return false;
}
}
bool should_include(const std::string& mimetype, const std::vector<std::string>& include)
{
return include.size() == 0 || std::find(include.cbegin(), include.cend(), mimetype) != include.end();
}
bool should_exclude(const std::string& mimetype, const std::vector<std::string>& exclude)
{
return exclude.size() != 0 && std::find(exclude.cbegin(), exclude.cend(), mimetype) != exclude.end();
}
void compute_repr(
const py::object& obj, const std::string& repr_method, const std::string& mimetype,
const std::vector<std::string>& include, const std::vector<std::string>& exclude,
py::dict& pub_data, py::dict& pub_metadata)
{
if (hasattr(obj, repr_method.c_str()) && should_include(mimetype, include) && !should_exclude(mimetype, exclude))
{
const py::object& repr = obj.attr(repr_method.c_str())();
if (!repr.is_none())
{
if (py::isinstance<py::tuple>(repr))
{
py::tuple repr_tuple = repr;
pub_data[mimetype.c_str()] = repr_tuple[0];
pub_metadata[mimetype.c_str()] = repr_tuple[1];
}
else
{
pub_data[mimetype.c_str()] = repr;
}
}
}
}
py::tuple mime_bundle_repr(const py::object& obj, const std::vector<std::string>& include = {}, const std::vector<std::string>& exclude = {})
{
py::module py_json = py::module::import("json");
py::module builtins = py::module::import("builtins");
py::dict pub_data;
py::dict pub_metadata;
if (hasattr(obj, "_repr_mimebundle_"))
{
pub_data = obj.attr("_repr_mimebundle_")(include, exclude);
}
else
{
compute_repr(obj, "_repr_html_", "text/html", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_markdown_", "text/markdown", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_svg_", "image/svg+xml", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_png_", "image/png", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_jpeg_", "image/jpeg", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_latex_", "text/latex", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_json_", "application/json", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_javascript_", "application/javascript", include, exclude, pub_data, pub_metadata);
compute_repr(obj, "_repr_pdf_", "application/pdf", include, exclude, pub_data, pub_metadata);
}
pub_data["text/plain"] = py::str(builtins.attr("repr")(obj));
return py::make_tuple(pub_data, pub_metadata);
}
/****************************
* xdisplayhook declaration *
****************************/
class xdisplayhook
{
public:
xdisplayhook();
virtual ~xdisplayhook();
void set_execution_count(int execution_count);
void operator()(const py::object& obj, bool raw) const;
private:
int m_execution_count;
};
/*******************************
* xdisplayhook implementation *
*******************************/
xdisplayhook::xdisplayhook()
: m_execution_count(0)
{
}
xdisplayhook::~xdisplayhook()
{
}
void xdisplayhook::set_execution_count(int execution_count)
{
m_execution_count = execution_count;
}
void xdisplayhook::operator()(const py::object& obj, bool raw = false) const
{
auto& interp = xeus::get_interpreter();
if (!obj.is_none())
{
if (hasattr(obj, "_ipython_display_"))
{
obj.attr("_ipython_display_")();
return;
}
py::object pub_data;
py::object pub_metadata;
if (raw)
{
pub_data = obj;
}
else
{
const py::tuple& repr = mime_bundle_repr(obj);
pub_data = repr[0];
pub_metadata = repr[1];
}
interp.publish_execution_result(m_execution_count, pub_data, pub_metadata);
}
}
/***************************
* xdisplay implementation *
***************************/
void xdisplay_impl(const py::args objs,
const std::vector<std::string>& include,
const std::vector<std::string>& exclude,
const py::dict& metadata,
const py::object& transient,
const py::object& display_id,
bool update,
bool raw)
{
auto& interp = xeus::get_interpreter();
for (std::size_t i = 0; i < objs.size(); ++i)
{
py::object obj = objs[i];
if (!obj.is_none())
{
if (hasattr(obj, "_ipython_display_"))
{
obj.attr("_ipython_display_")();
return;
}
py::object pub_data;
py::object pub_metadata = py::dict();
if (raw)
{
pub_data = obj;
}
else
{
const py::tuple& repr = mime_bundle_repr(obj, include, exclude);
pub_data = repr[0];
pub_metadata = repr[1];
}
pub_metadata.attr("update")(metadata);
nl::json cpp_transient = transient.is_none() ? nl::json::object() : nl::json(transient);
if (!display_id.is_none())
{
cpp_transient["display_id"] = display_id;
}
if (update)
{
interp.update_display_data(pub_data, pub_metadata, std::move(cpp_transient));
}
else
{
interp.display_data(pub_data, pub_metadata, std::move(cpp_transient));
}
}
}
}
void xdisplay(py::args objs, py::kwargs kw)
{
auto raw = kw.contains("raw") ? kw["raw"].cast<bool>() : false;
auto include = kw.contains("include") ? kw["include"].cast<std::vector<std::string>>() : std::vector<std::string>();
auto exclude = kw.contains("exclude") ? kw["exclude"].cast<std::vector<std::string>>() : std::vector<std::string>({});
auto metadata = kw.contains("metadata") ? py::object(kw["metadata"]) : py::dict();
auto transient = kw.contains("transient") ? py::object(kw["transient"]) : py::none();
auto display_id = kw.contains("display_id") ? py::object(kw["display_id"]) : py::none();
xdisplay_impl(objs, include, exclude, metadata, transient, display_id, false, raw);
}
void xupdate_display(py::args objs, py::kwargs kw)
{
auto raw = kw.contains("raw") ? kw["raw"].cast<bool>() : false;
auto include = kw.contains("include") ? kw["include"].cast<std::vector<std::string>>() : std::vector<std::string>();
auto exclude = kw.contains("exclude") ? kw["exclude"].cast<std::vector<std::string>>() : std::vector<std::string>({});
auto metadata = kw.contains("metadata") ? py::object(kw["metadata"]) : py::dict();
auto transient = kw.contains("transient") ? py::object(kw["transient"]) : py::none();
auto display_id = kw.contains("display_id") ? py::object(kw["display_id"]) : py::none();
xdisplay_impl(objs, include, exclude, metadata, transient, display_id, true, raw);
}
void xpublish_display_data(const py::object& data, const py::object& metadata, const py::str& /*source*/, const py::object& transient)
{
auto& interp = xeus::get_interpreter();
interp.display_data(data, metadata, transient);
}
void xdisplay_mimetype(const std::string& mimetype, py::args objs, py::kwargs kw)
{
bool raw = kw.contains("raw") ? kw["raw"].cast<bool>() : false;
py::object metadata = kw.contains("metadata") ? kw["metadata"] : py::dict();
py::object p_metadata = py::dict();
if (!metadata.is_none())
{
p_metadata = py::dict(py::arg(mimetype.c_str()) = metadata);
}
py::list disp_objs = objs;
if (raw)
{
for (std::size_t i = 0; i < objs.size(); ++i)
{
disp_objs[i] = py::dict(py::arg(mimetype.c_str()) = objs[i]);
}
}
xdisplay_impl(disp_objs, { mimetype }, {}, p_metadata, py::none(), py::none(), false, raw);
}
void xdisplay_html(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("text/html", objs, kw);
}
void xdisplay_markdown(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("text/markdown", objs, kw);
}
void xdisplay_svg(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("image/svg+xml", objs, kw);
}
void xdisplay_png(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("image/png", objs, kw);
}
void xdisplay_jpeg(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("image/jpeg", objs, kw);
}
void xdisplay_latex(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("text/latex", objs, kw);
}
void xdisplay_json(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("application/json", objs, kw);
}
void xdisplay_javascript(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("application/javascript", objs, kw);
}
void xdisplay_pdf(py::args objs, py::kwargs kw)
{
xdisplay_mimetype("application/pdf", objs, kw);
}
/*************************
* xclear implementation *
*************************/
void xclear(bool wait = false)
{
auto& interp = xeus::get_interpreter();
interp.clear_output(wait);
}
/*******************************
* xdisplay_object declaration *
*******************************/
class xdisplay_object
{
public:
xdisplay_object(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata = py::dict(), const std::string& read_flag = "r");
virtual ~xdisplay_object();
xdisplay_object(const xdisplay_object&) = delete;
xdisplay_object(xdisplay_object&&) = delete;
xdisplay_object& operator=(const xdisplay_object&) = delete;
xdisplay_object& operator=(xdisplay_object&&) = delete;
void reload();
py::object get_metadata();
virtual void set_metadata(const py::object& data);
py::object get_data();
virtual void set_data(const py::object& data);
protected:
py::object data_and_metadata() const;
private:
py::object m_data;
py::object m_url = py::none();
py::object m_filename = py::none();
py::object m_metadata = py::none();
py::str m_read_flag;
};
/**********************************
* xdisplay_object implementation *
**********************************/
xdisplay_object::xdisplay_object(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata, const std::string& read_flag)
: m_data(data), m_url(url), m_filename(filename), m_metadata(metadata), m_read_flag(read_flag)
{
py::module pathlib = py::module::import("pathlib");
if (py::isinstance(data, py::make_tuple(pathlib.attr("Path"), pathlib.attr("PurePath"))))
{
m_data = py::str(data);
}
if (!m_data.is_none() && py::isinstance<py::str>(m_data))
{
if (xpyt::is_pyobject_true(m_data.attr("startswith")("http")) && url.is_none())
{
m_url = m_data;
m_filename = py::none();
m_data = py::none();
}
else if (safe_exists(m_data) && filename.is_none())
{
m_url = py::none();
m_filename = m_data;
m_data = py::none();
}
}
reload();
}
xdisplay_object::~xdisplay_object()
{
}
py::object xdisplay_object::data_and_metadata() const
{
py::module copy = py::module::import("copy");
if (m_metadata.is_none())
{
return m_data;
}
else
{
return py::make_tuple(m_data, copy.attr("deepcopy")(m_metadata));
}
}
py::object xdisplay_object::get_metadata()
{
return m_metadata;
}
void xdisplay_object::set_metadata(const py::object& data)
{
m_metadata = data;
}
py::object xdisplay_object::get_data()
{
return m_data;
}
void xdisplay_object::set_data(const py::object& data)
{
m_data = data;
}
void xdisplay_object::reload()
{
py::module builtins = py::module::import("builtins");
if (!m_filename.is_none())
{
py::object fobj;
try
{
fobj = builtins.attr("open")(m_filename, m_read_flag);
set_data(fobj.attr("read")());
}
catch (py::error_already_set& e)
{
fobj.attr("close")();
throw e;
}
}
else if (!m_url.is_none())
{
try
{
py::module request = py::module::import("urllib.request");
py::object response = request.attr("urlopen")(m_url);
py::object content = response.attr("read")();
py::object encoding = py::none();
for (py::handle sub : response.attr("headers")["content-type"].attr("split")(";"))
{
sub = sub.attr("strip")();
if (xpyt::is_pyobject_true(sub.attr("startswith")("charset")))
{
py::list splitted = sub.attr("split")("=");
encoding = splitted[py::len(splitted) - 1].attr("strip")();
break;
}
}
if (!encoding.is_none())
{
set_data(content.attr("decode")(encoding, "replace"));
}
else
{
set_data(content);
}
}
catch (py::error_already_set& e)
{
set_data(py::none());
}
}
}
/******************************
* xtext_display_object class *
******************************/
class xtext_display_object : public xdisplay_object
{
public:
xtext_display_object(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata = py::dict());
virtual ~xtext_display_object();
};
xtext_display_object::xtext_display_object(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata)
{
}
xtext_display_object::~xtext_display_object()
{
}
/***************
* xhtml class *
***************/
class xhtml : public xdisplay_object
{
public:
xhtml(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata);
virtual ~xhtml();
py::object repr_html() const;
py::object html() const;
};
xhtml::xhtml(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata)
{
}
xhtml::~xhtml()
{
}
py::object xhtml::repr_html() const
{
return data_and_metadata();
}
py::object xhtml::html() const
{
return repr_html();
}
/*********************
* xjavascript class *
*********************/
class xjavascript : public xtext_display_object
{
public:
xjavascript(const py::object& data, const py::object& url, const py::object& filename, const py::object& lib, const py::object& css);
virtual ~xjavascript();
py::object repr_javascript();
private:
py::list m_lib;
py::list m_css;
};
xjavascript::xjavascript(const py::object& data, const py::object& url, const py::object& filename, const py::object& lib, const py::object& css)
: xtext_display_object(data, url, filename)
{
if (py::isinstance<py::str>(lib))
{
m_lib = py::list({ lib });
}
else if (lib.is_none())
{
m_lib = py::list();
}
else
{
m_lib = lib;
}
if (py::isinstance<py::str>(css))
{
m_css = py::list({ css });
}
else if (css.is_none())
{
m_css = py::list();
}
else
{
m_css = css;
}
}
xjavascript::~xjavascript()
{
}
py::object xjavascript::repr_javascript()
{
std::ostringstream string_stream;
for (py::handle css : m_css)
{
string_stream << R"($("head").append($("<link/>").attr({rel: "stylesheet", type: "text/css", href: ")";
string_stream << css.cast<std::string>();
string_stream << R"("})))";
}
for (py::handle lib : m_lib)
{
string_stream << R"($.getScript(")";
string_stream << lib.cast<std::string>();
string_stream << R"(", function () {)";
}
string_stream << get_data().cast<std::string>();
for (std::size_t i = 0; i < py::len(m_lib); i++)
{
string_stream << "});";
}
return py::str(string_stream.str());
}
/*******************
* xmarkdown class *
*******************/
class xmarkdown : public xdisplay_object
{
public:
xmarkdown(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata);
virtual ~xmarkdown();
py::object repr_markdown() const;
};
xmarkdown::xmarkdown(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata)
{
}
xmarkdown::~xmarkdown()
{
}
py::object xmarkdown::repr_markdown() const
{
return data_and_metadata();
}
/***************
* xmath class *
***************/
class xmath : public xdisplay_object
{
public:
xmath(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata);
virtual ~xmath();
py::object repr_latex();
};
xmath::xmath(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata)
{
}
xmath::~xmath()
{
}
py::object xmath::repr_latex()
{
py::module copy = py::module::import("copy");
std::ostringstream string_stream;
string_stream << R"($\displaystyle )" << get_data().attr("strip")("$").cast<std::string>() << "$";
py::str s = py::str(string_stream.str());
if (get_metadata().is_none())
{
return s;
}
else
{
return py::make_tuple(s, copy.attr("deepcopy")(get_metadata()));
}
}
/****************
* xlatex class *
****************/
class xlatex : public xdisplay_object
{
public:
xlatex(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata);
virtual ~xlatex();
py::object repr_latex() const;
};
xlatex::xlatex(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata)
{
}
xlatex::~xlatex()
{
}
py::object xlatex::repr_latex() const
{
return data_and_metadata();
}
/**************
* xsvg class *
**************/
class xsvg : public xdisplay_object
{
public:
xsvg(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata);
virtual ~xsvg();
py::object repr_svg() const;
protected:
void set_data(const py::object& data) override;
};
xsvg::xsvg(const py::object& data, const py::object& url, const py::object& filename, const py::object& metadata)
: xdisplay_object(data, url, filename, metadata, "rb")
{
}
xsvg::~xsvg()
{
}
void xsvg::set_data(const py::object& data)
{
if (data.is_none())
{
xdisplay_object::set_data(data);
return;
}
py::object svg = data;
py::module minidom = py::module::import("xml.dom.minidom");
py::list found_svg = minidom.attr("parseString")(data).attr("getElementsByTagName")("svg");
if (py::len(found_svg) != 0)
{
svg = found_svg[0].attr("toxml")();
}
xdisplay_object::set_data(svg);
}
py::object xsvg::repr_svg() const
{
return data_and_metadata();
}
/***************
* xjson class *
***************/
class xjson : public xdisplay_object
{
public:
xjson(
const py::object& data, const py::object& url, const py::object& filename,
const py::bool_& expanded, const py::object& metadata, const py::str& root
);
virtual ~xjson();
py::object repr_json() const;
protected:
void set_data(const py::object& data) override;
};
xjson::xjson(
const py::object& data, const py::object& url, const py::object& filename,
const py::bool_& expanded, const py::object& metadata, const py::str& root)
: xdisplay_object(data, url, filename, metadata)
{
if (get_metadata().is_none())
{
set_metadata(py::dict("expanded"_a = expanded, "root"_a = root));
}
else
{
get_metadata().attr("update")(py::dict("expanded"_a = expanded, "root"_a = root));
}
}
xjson::~xjson()
{
}
void xjson::set_data(const py::object& data)
{
py::module pathlib = py::module::import("pathlib");
if (py::isinstance(data, py::make_tuple(pathlib.attr("Path"), pathlib.attr("PurePath"))))
{
xdisplay_object::set_data(py::str(data));
return;
}
if (py::isinstance<py::str>(data))
{
py::module json = py::module::import("json");
xdisplay_object::set_data(json.attr("loads")(data));
return;
}
xdisplay_object::set_data(data);
}
py::object xjson::repr_json() const
{
return data_and_metadata();
}
/******************
* xgeojson class *
******************/
class xgeojson : public xjson
{
public:
xgeojson(
const py::object& data, const py::object& url, const py::object& filename,
const py::bool_& expanded, const py::object& metadata, const py::str& root,
const py::dict& layer_options, const py::str& url_template
);
virtual ~xgeojson();
void ipython_display();
private:
py::dict m_layer_options;
py::str m_url_template;
};
xgeojson::xgeojson(
const py::object& data, const py::object& url, const py::object& filename,
const py::bool_& expanded, const py::object& metadata, const py::str& root,
const py::dict& layer_options, const py::str& url_template)
: xjson(data, url, filename, expanded, metadata, root), m_layer_options(layer_options), m_url_template(url_template)
{
const py::dict& p_metadata = get_metadata();
p_metadata["layer_options"] = m_layer_options;
p_metadata["url_template"] = m_url_template;
}
xgeojson::~xgeojson()
{
}
void xgeojson::ipython_display()