Guest User

Untitled

a guest
Jun 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Make your object repr pretty in Jupyter"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "class C(object):\n",
  17. " def _repr_html_(self):\n",
  18. " return \"\"\"<p><i>HTML <span style=\"color:red\">WORKED</span></i></p>\"\"\""
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 2,
  24. "metadata": {},
  25. "outputs": [
  26. {
  27. "data": {
  28. "text/plain": [
  29. "__main__.C"
  30. ]
  31. },
  32. "execution_count": 2,
  33. "metadata": {},
  34. "output_type": "execute_result"
  35. }
  36. ],
  37. "source": [
  38. "# class itself not affected\n",
  39. "C"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 3,
  45. "metadata": {},
  46. "outputs": [],
  47. "source": [
  48. "c = C()"
  49. ]
  50. },
  51. {
  52. "cell_type": "code",
  53. "execution_count": 4,
  54. "metadata": {},
  55. "outputs": [
  56. {
  57. "data": {
  58. "text/html": [
  59. "<p><i>HTML <span style=\"color:red\">WORKED</span></i></p>"
  60. ],
  61. "text/plain": [
  62. "<__main__.C at 0x1040298d0>"
  63. ]
  64. },
  65. "execution_count": 4,
  66. "metadata": {},
  67. "output_type": "execute_result"
  68. }
  69. ],
  70. "source": [
  71. "# instance is represented by result of `_repr_html_`\n",
  72. "c"
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 5,
  78. "metadata": {},
  79. "outputs": [
  80. {
  81. "name": "stdout",
  82. "output_type": "stream",
  83. "text": [
  84. "<__main__.C object at 0x1040298d0>\n"
  85. ]
  86. }
  87. ],
  88. "source": [
  89. "# But print or string representation do not call `_repr_html_`\n",
  90. "print(c)"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": 6,
  96. "metadata": {},
  97. "outputs": [
  98. {
  99. "data": {
  100. "text/plain": [
  101. "'<__main__.C object at 0x1040298d0>'"
  102. ]
  103. },
  104. "execution_count": 6,
  105. "metadata": {},
  106. "output_type": "execute_result"
  107. }
  108. ],
  109. "source": [
  110. "str(c)"
  111. ]
  112. }
  113. ],
  114. "metadata": {
  115. "kernelspec": {
  116. "display_name": "Python 3.6",
  117. "language": "python",
  118. "name": "py36"
  119. },
  120. "language_info": {
  121. "codemirror_mode": {
  122. "name": "ipython",
  123. "version": 3
  124. },
  125. "file_extension": ".py",
  126. "mimetype": "text/x-python",
  127. "name": "python",
  128. "nbconvert_exporter": "python",
  129. "pygments_lexer": "ipython3",
  130. "version": "3.6.3"
  131. }
  132. },
  133. "nbformat": 4,
  134. "nbformat_minor": 2
  135. }
Add Comment
Please, Sign In to add comment