diff --git a/tensorboard/plugins/core/core_plugin.py b/tensorboard/plugins/core/core_plugin.py index 770b03ba17..79dbb68996 100644 --- a/tensorboard/plugins/core/core_plugin.py +++ b/tensorboard/plugins/core/core_plugin.py @@ -447,6 +447,25 @@ def define_flags(self, parser): means keep all samples of that type. For instance "scalars=500,images=0" keeps 500 scalars and all images. Most users should not need to set this flag.\ +''') + + parser.add_argument( + '--url_file', + metavar='FILE', + type=str, + help='''\ +An optional file to which to write a URL from which TensorBoard should +be reachable. The URL will include scheme, hostname, and port, and +possibly path if `--path_prefix` is set. Useful when setting `--port=0`, +which automatically selects an unused port. Primarily intended for use +by scripts; for human consumers, this same value is already printed to +stderr. + +If provided, this should point to an existing file (which will be +truncated) or a nonexistent file within a directory that exists. + +If TensorBoard fails to start (e.g., because the specified port is +already in use), this file will not be written. ''') def fix_flags(self, flags): diff --git a/tensorboard/program.py b/tensorboard/program.py index 96f84ac4f9..ab39cca64d 100644 --- a/tensorboard/program.py +++ b/tensorboard/program.py @@ -207,9 +207,13 @@ def main(self, ignored_argv=('',)): return 0 try: server = self._make_server() + url = server.get_url() sys.stderr.write('TensorBoard %s at %s (Press CTRL+C to quit)\n' % - (version.VERSION, server.get_url())) + (version.VERSION, url)) sys.stderr.flush() + if self.flags.url_file is not None: + with open(self.flags.url_file, "w") as outfile: + outfile.write("%s\n" % server.get_url()) server.serve_forever() return 0 except TensorBoardServerException as e: