|
70 | 70 |
|
71 | 71 | import os
|
72 | 72 | import sys
|
| 73 | +import io |
| 74 | + |
| 75 | + |
| 76 | +def _is_direct_output(stream=sys.stdout): |
| 77 | + """Checks if output stream redirects to the shell/console directly.""" |
| 78 | + |
| 79 | + if stream.isatty(): |
| 80 | + return True |
| 81 | + if isinstance(stream, io.TextIOWrapper): |
| 82 | + return _is_direct_output(stream.buffer) |
| 83 | + if isinstance(stream, io.BufferedWriter): |
| 84 | + return _is_direct_output(stream.raw) |
| 85 | + if isinstance(stream, io.FileIO): |
| 86 | + return stream.fileno() in [1, 2] |
| 87 | + return False |
| 88 | + |
73 | 89 |
|
74 | 90 | # Only print the welcome message if TFDF_DISABLE_WELCOME_MESSAGE is not set and
|
75 |
| -# if user has notalready imported YDF. |
| 91 | +# if user has not already imported YDF. |
76 | 92 | if (
|
77 | 93 | os.getenv("TFDF_DISABLE_WELCOME_MESSAGE") is None
|
78 | 94 | and "ydf" not in sys.modules
|
79 | 95 | ):
|
80 | 96 |
|
81 |
| - if ( |
82 |
| - "IPython" in sys.modules and "google.colab" in sys.modules |
83 |
| - ): # Check if executed in a Notebook |
| 97 | + if not _is_direct_output(): # Check if executed in a Notebook |
84 | 98 |
|
85 | 99 | import IPython
|
86 | 100 |
|
87 | 101 | IPython.display.display(IPython.display.HTML("""
|
88 | 102 | <p style="margin:0px;">🌲 Try <a href="https://ydf.readthedocs.io/en/latest/" target="_blank">YDF</a>, the successor of
|
89 | 103 | <a href="https://www.tensorflow.org/decision_forests" target="_blank">TensorFlow
|
90 |
| - Decision Forests</a> using the same algorithm but with more features and faster |
| 104 | + Decision Forests</a> using the same algorithms but with more features and faster |
91 | 105 | training!
|
92 | 106 | </p>
|
93 | 107 | <div style="display: flex; flex-wrap: wrap; margin:5px;max-width: 880px;">
|
|
121 | 135 | """))
|
122 | 136 |
|
123 | 137 | else:
|
| 138 | + import termcolor |
| 139 | + print( |
| 140 | + termcolor.colored("🌲 Try ", "green"), |
| 141 | + termcolor.colored("https://ydf.readthedocs.io", "blue"), |
| 142 | + termcolor.colored( |
| 143 | + ", the successor of TensorFlow Decision Forests with more" |
| 144 | + " features and faster training!", |
| 145 | + "green", |
| 146 | + ), |
| 147 | + sep="", |
| 148 | + file=sys.stderr, |
| 149 | + ) |
124 | 150 | pass
|
0 commit comments