diff --git a/hatch_build.py b/hatch_build.py index 9a695be06..ab12d1b19 100644 --- a/hatch_build.py +++ b/hatch_build.py @@ -31,7 +31,7 @@ def initialize(self, version, build_data): if not os.path.exists(".git") and os.path.exists(dest): # not running from git, nothing to do return - print("Downloading CSS: %s" % url) + print(f"Downloading CSS: {url}") try: css = urlopen(url).read() except Exception as e: @@ -39,11 +39,11 @@ def initialize(self, version, build_data): print(msg, file=sys.stderr) if os.path.exists(dest): - print("Already have CSS: %s, moving on." % dest) + print(f"Already have CSS: {dest}, moving on.") else: raise OSError("Need CSS to proceed.") return with open(dest, "wb") as f: f.write(css) - print("Downloaded Notebook CSS to %s" % dest) + print(f"Downloaded Notebook CSS to {dest}") diff --git a/tests/app/cgi-test.py b/tests/app/cgi-test.py index 2728d67f1..8721059f1 100644 --- a/tests/app/cgi-test.py +++ b/tests/app/cgi-test.py @@ -10,7 +10,7 @@ def notebook_cgi_path(base_url): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory}", *voila_args_extra] async def test_cgi_using_query_parameters(http_server_client, notebook_cgi_path): diff --git a/tests/app/config_paths_test.py b/tests/app/config_paths_test.py index 87f42484c..f412798ac 100644 --- a/tests/app/config_paths_test.py +++ b/tests/app/config_paths_test.py @@ -9,7 +9,7 @@ @pytest.fixture def voila_config_file_paths_arg(): path = os.path.join(BASE_DIR, "..", "configs", "general") - return "--VoilaTest.config_file_paths=[%r]" % path + return f"--VoilaTest.config_file_paths=[{path!r}]" def test_config_app(voila_app): diff --git a/tests/app/contents_handler_test.py b/tests/app/contents_handler_test.py index a8f7f75f8..afca933ec 100644 --- a/tests/app/contents_handler_test.py +++ b/tests/app/contents_handler_test.py @@ -15,7 +15,7 @@ def contents_prefix(base_url): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] @pytest.fixture diff --git a/tests/app/cwd_subdir_test.py b/tests/app/cwd_subdir_test.py index 530f124f9..3ca173e46 100644 --- a/tests/app/cwd_subdir_test.py +++ b/tests/app/cwd_subdir_test.py @@ -6,12 +6,12 @@ @pytest.fixture def cwd_subdir_notebook_url(base_url): - return base_url + f"voila/render/{NOTEBOOK_PATH}" + return f"{base_url}voila/render/{NOTEBOOK_PATH}" @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_hello_world(http_server_client, cwd_subdir_notebook_url): diff --git a/tests/app/execute_cpp_test.py b/tests/app/execute_cpp_test.py index ef3c6513b..3d2c06348 100644 --- a/tests/app/execute_cpp_test.py +++ b/tests/app/execute_cpp_test.py @@ -8,7 +8,7 @@ @pytest.fixture def cpp_file_url(base_url, preheat_mode): - return base_url + f"voila/render/{NOTEBOOK_PATH}" + return f"{base_url}voila/render/{NOTEBOOK_PATH}" @pytest.fixture @@ -21,7 +21,7 @@ def voila_args_extra(): @pytest.fixture def voila_args(notebook_directory, voila_args_extra, preheat_mode): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] @pytest.mark.skipif( diff --git a/tests/app/nbextensions_test.py b/tests/app/nbextensions_test.py index 24634c199..735acae4e 100644 --- a/tests/app/nbextensions_test.py +++ b/tests/app/nbextensions_test.py @@ -20,7 +20,7 @@ def config(app): def voila_config_file_paths_arg(): # we don't want the tests to use any configuration on the system path = os.path.abspath(os.path.join(BASE_DIR, "../configs/general")) - return "--VoilaTest.config_file_paths=[%r]" % path + return f"--VoilaTest.config_file_paths=[{path!r}]" @pytest.mark.skip(reason="TODO: update for JupyterLab extensions") diff --git a/tests/app/no_kernelspec_test.py b/tests/app/no_kernelspec_test.py index 775fdc7d8..8f252c7fa 100644 --- a/tests/app/no_kernelspec_test.py +++ b/tests/app/no_kernelspec_test.py @@ -5,12 +5,12 @@ @pytest.fixture def non_existing_kernel_notebook(base_url): - return base_url + f"voila/render/{NOTEBOOK_PATH}" + return f"{base_url}voila/render/{NOTEBOOK_PATH}" @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_non_existing_kernel( diff --git a/tests/app/no_metadata.py b/tests/app/no_metadata.py index fe8fad17a..50c1b61b0 100644 --- a/tests/app/no_metadata.py +++ b/tests/app/no_metadata.py @@ -10,7 +10,7 @@ def non_existing_notebook_metadata(base_url): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_non_existing_metadata( diff --git a/tests/app/non_existing_kernel_test.py b/tests/app/non_existing_kernel_test.py index 079cf99a5..c16be95d4 100644 --- a/tests/app/non_existing_kernel_test.py +++ b/tests/app/non_existing_kernel_test.py @@ -5,12 +5,12 @@ @pytest.fixture def non_existing_kernel_notebook(base_url): - return base_url + f"voila/render/{NOTEBOOK_PATH}" + return f"{base_url}voila/render/{NOTEBOOK_PATH}" @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_non_existing_kernel(http_server_client, non_existing_kernel_notebook): diff --git a/tests/app/notebooks_test.py b/tests/app/notebooks_test.py index 760603061..7070f68a4 100644 --- a/tests/app/notebooks_test.py +++ b/tests/app/notebooks_test.py @@ -11,7 +11,7 @@ def notebook_other_comms_path(base_url): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_other_comms(http_server_client, notebook_other_comms_path): diff --git a/tests/app/preheat_configuration_test.py b/tests/app/preheat_configuration_test.py index 53cabe9f2..c2e9d1a86 100644 --- a/tests/app/preheat_configuration_test.py +++ b/tests/app/preheat_configuration_test.py @@ -13,7 +13,7 @@ @pytest.fixture def voila_config_file_paths_arg(): path = os.path.join(BASE_DIR, "..", "configs", "preheat") - return "--VoilaTest.config_file_paths=[%r]" % path + return f"--VoilaTest.config_file_paths=[{path!r}]" @pytest.fixture diff --git a/tests/app/preheat_multiple_notebooks_test.py b/tests/app/preheat_multiple_notebooks_test.py index 8e34f6131..bd395a2f2 100644 --- a/tests/app/preheat_multiple_notebooks_test.py +++ b/tests/app/preheat_multiple_notebooks_test.py @@ -13,7 +13,7 @@ @pytest.fixture def voila_config_file_paths_arg(): path = os.path.join(BASE_DIR, "..", "configs", "preheat") - return "--VoilaTest.config_file_paths=[%r]" % path + return f"--VoilaTest.config_file_paths=[{path!r}]" @pytest.fixture diff --git a/tests/app/serve_directory_test.py b/tests/app/serve_directory_test.py index bac1d5146..a3e75e968 100644 --- a/tests/app/serve_directory_test.py +++ b/tests/app/serve_directory_test.py @@ -13,7 +13,7 @@ def preheat_mode(): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_print(http_server_client, print_notebook_url): diff --git a/tests/app/show_traceback_test.py b/tests/app/show_traceback_test.py index bcdd8353e..cea9796f4 100644 --- a/tests/app/show_traceback_test.py +++ b/tests/app/show_traceback_test.py @@ -16,7 +16,7 @@ def notebook_show_traceback_path(base_url): @pytest.fixture def voila_args(notebook_directory, voila_args_extra, show_tracebacks): return [ - "--VoilaTest.root_dir=%r" % notebook_directory, + f"--VoilaTest.root_dir={notebook_directory!r}", f"--VoilaConfiguration.show_tracebacks={show_tracebacks}", *voila_args_extra, ] diff --git a/tests/app/template_cli_test.py b/tests/app/template_cli_test.py index acd140e7d..bb2bbefb3 100644 --- a/tests/app/template_cli_test.py +++ b/tests/app/template_cli_test.py @@ -18,9 +18,7 @@ def voila_args_extra(): ) return [ "--template=test_template", - "--VoilaTest.template_paths=[{!r}, {!r}]".format( - path_test_template, path_default - ), + f"--VoilaTest.template_paths=[{path_test_template!r}, {path_default!r}]", "--VoilaExecutor.timeout=240", ] diff --git a/tests/app/tree_test.py b/tests/app/tree_test.py index 70c063690..c1f350277 100644 --- a/tests/app/tree_test.py +++ b/tests/app/tree_test.py @@ -9,7 +9,7 @@ def preheat_mode(): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] @pytest.fixture diff --git a/tests/conftest.py b/tests/conftest.py index b56e6411e..3a66e3233 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,7 +19,7 @@ def notebook_directory(): @pytest.fixture def print_notebook_url(base_url): - return base_url + "voila/render/print.ipynb" + return f"{base_url}voila/render/print.ipynb" @pytest.fixture diff --git a/tests/server/cwd_subdir_test.py b/tests/server/cwd_subdir_test.py index 35b9f4271..7ee0e95fa 100644 --- a/tests/server/cwd_subdir_test.py +++ b/tests/server/cwd_subdir_test.py @@ -5,12 +5,12 @@ @pytest.fixture def cwd_subdir_notebook_url(base_url): - return base_url + "voila/render/subdir/cwd_subdir.ipynb" + return f"{base_url}voila/render/subdir/cwd_subdir.ipynb" @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] async def test_hello_world(http_server_client, cwd_subdir_notebook_url): diff --git a/tests/server/execute_cpp_test.py b/tests/server/execute_cpp_test.py index 168b02f0e..727d43eff 100644 --- a/tests/server/execute_cpp_test.py +++ b/tests/server/execute_cpp_test.py @@ -7,7 +7,7 @@ @pytest.fixture def cpp_file_url(base_url): - return base_url + "voila/render/print.xcpp" + return f"{base_url}voila/render/print.xcpp" @pytest.fixture @@ -17,7 +17,7 @@ def jupyter_server_args_extra(): @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] @pytest.mark.skipif( diff --git a/tests/server/tree_test.py b/tests/server/tree_test.py index d660bbf49..d7b3660ea 100644 --- a/tests/server/tree_test.py +++ b/tests/server/tree_test.py @@ -4,7 +4,7 @@ @pytest.fixture def voila_args(notebook_directory, voila_args_extra): - return ["--VoilaTest.root_dir=%r" % notebook_directory, *voila_args_extra] + return [f"--VoilaTest.root_dir={notebook_directory!r}", *voila_args_extra] @pytest.fixture @@ -16,7 +16,7 @@ def jupyter_server_args_extra(): async def test_tree(http_server_client, base_url): - response = await http_server_client.fetch(base_url + "voila/tree") + response = await http_server_client.fetch(f"{base_url}voila/tree") assert response.code == 200 text = response.body.decode("utf-8") assert "print.ipynb" in text, "tree handler should render ipynb files" diff --git a/voila/app.py b/voila/app.py index 3ca9b22a6..24724effd 100644 --- a/voila/app.py +++ b/voila/app.py @@ -387,7 +387,10 @@ def display_url(self): if not url.endswith("/"): url += "/" else: - ip = "%s" % socket.gethostname() if self.ip in ("", "0.0.0.0") else self.ip + if self.ip in ("", "0.0.0.0"): + ip = socket.gethostname() + else: + ip = self.ip url = self._url(ip) # TODO: do we want to have the token? if self.identity_provider.token: @@ -411,7 +414,7 @@ def _url(self, ip): # TODO: https / certfile # proto = 'https' if self.certfile else 'http' proto = "http" - return "%s://%s:%i%s" % (proto, ip, self.port, self.base_url) + return f"{proto}://{ip}:{self.port}{self.base_url}" config_file_paths = List( Unicode(), config=True, help=_("Paths to search for voila.(py|json)") @@ -526,10 +529,10 @@ def initialize(self, argv=None): self.notebook_path = arg else: raise ValueError( - "argument is neither a file nor a directory: %r" % arg + f"argument is neither a file nor a directory: {arg!r}" ) elif len(self.extra_args) != 0: - raise ValueError("provided more than 1 argument: %r" % self.extra_args) + raise ValueError(f"provided more than 1 argument: {self.extra_args!r}") # then we load the config self.load_config_file("voila", path=self.config_file_paths) @@ -569,7 +572,7 @@ def setup_template_dirs(self): self.log.debug("template paths:\n\t%s", "\n\t".join(self.template_paths)) self.log.debug("static paths:\n\t%s", "\n\t".join(self.static_paths)) if self.notebook_path and not os.path.exists(self.notebook_path): - raise ValueError("Notebook not found: %s" % self.notebook_path) + raise ValueError(f"Notebook not found: {self.notebook_path}") def init_settings(self) -> Dict: """Initialize settings for Voila application.""" @@ -684,14 +687,12 @@ def init_handlers(self) -> List: handlers.extend( [ ( - url_path_join( - self.server_url, r"/api/kernels/%s" % _kernel_id_regex - ), + url_path_join(self.server_url, rf"/api/kernels/{_kernel_id_regex}"), KernelHandler, ), ( url_path_join( - self.server_url, r"/api/kernels/%s/channels" % _kernel_id_regex + self.server_url, rf"/api/kernels/{_kernel_id_regex}/channels" ), KernelWebsocketHandler, ), @@ -724,9 +725,7 @@ def init_handlers(self) -> List: if self.voila_configuration.preheat_kernel: handlers.append( ( - url_path_join( - self.server_url, r"/voila/query/%s" % _kernel_id_regex - ), + url_path_join(self.server_url, rf"/voila/query/{_kernel_id_regex}"), RequestInfoSocketHandler, ) ) @@ -775,7 +774,7 @@ def init_handlers(self) -> List: [ (self.server_url, TornadoVoilaTreeHandler, tree_handler_conf), ( - url_path_join(self.server_url, r"/voila/tree" + path_regex), + url_path_join(self.server_url, rf"/voila/tree{path_regex}"), TornadoVoilaTreeHandler, tree_handler_conf, ), @@ -792,7 +791,7 @@ def init_handlers(self) -> List: # On serving a directory, expose the content handler. ( url_path_join( - self.server_url, r"/voila/api/contents%s" % path_regex + self.server_url, rf"/voila/api/contents{path_regex}" ), VoilaContentsHandler, tree_handler_conf, diff --git a/voila/execute.py b/voila/execute.py index 6071725ee..e7f393908 100644 --- a/voila/execute.py +++ b/voila/execute.py @@ -118,9 +118,7 @@ def strip_code_cell_errors(self, cell): output for output in outputs if output["output_type"] == "error" ] - error_message = "There was an error when executing cell [{}]. {}".format( - cell["execution_count"], self.cell_error_instruction - ) + error_message = f'There was an error when executing cell [{cell["execution_count"]}]. {self.cell_error_instruction}' for output in error_outputs: output["ename"] = "ExecutionError" diff --git a/voila/notebook_renderer.py b/voila/notebook_renderer.py index 2ba094e59..079b8bb5f 100644 --- a/voila/notebook_renderer.py +++ b/voila/notebook_renderer.py @@ -397,5 +397,5 @@ async def find_kernel_name_for_language(self, kernel_language, kernel_specs=None return matches[0] else: raise tornado.web.HTTPError( - 500, "No Jupyter kernel for language %r found" % kernel_language + 500, f"No Jupyter kernel for language {kernel_language!r} found" ) diff --git a/voila/server_extension.py b/voila/server_extension.py index 959990c05..d57253088 100644 --- a/voila/server_extension.py +++ b/voila/server_extension.py @@ -120,7 +120,7 @@ def _load_jupyter_server_extension(server_app): }, ), ( - url_path_join(base_url, r"/voila/api/contents%s" % path_regex), + url_path_join(base_url, rf"/voila/api/contents{path_regex}"), VoilaContentsHandler, tree_handler_conf, ), diff --git a/voila/tornado/contentshandler.py b/voila/tornado/contentshandler.py index b49dfc3e3..3665ed861 100644 --- a/voila/tornado/contentshandler.py +++ b/voila/tornado/contentshandler.py @@ -34,10 +34,10 @@ async def get(self, path=""): format = self.get_query_argument("format", default=None) if format not in {None, "text", "base64"}: - raise web.HTTPError(400, "Format %r is invalid" % format) + raise web.HTTPError(400, f"Format {format!r} is invalid") content_str = self.get_query_argument("content", default="1") if content_str not in {"0", "1"}: - raise web.HTTPError(400, "Content %r is invalid" % content_str) + raise web.HTTPError(400, f"Content {content_str!r} is invalid") content = int(content_str or "") if not cm.allow_hidden and await ensure_async(cm.is_hidden(path)): diff --git a/voila/utils.py b/voila/utils.py index 7100a02af..d40134e61 100644 --- a/voila/utils.py +++ b/voila/utils.py @@ -74,7 +74,7 @@ def get_server_root_dir(settings): home = os.path.expanduser("~") if root_dir.startswith(home + os.path.sep): # collapse $HOME to ~ - root_dir = "~" + root_dir[len(home) :] + root_dir = f"~{root_dir[len(home):]}" return root_dir