Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use f-strings #419

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
6 changes: 3 additions & 3 deletions hatch_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ 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:
msg = f"Failed to download css from {url}: {e}"
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}")
2 changes: 1 addition & 1 deletion tests/app/cgi-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/config_paths_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/contents_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/app/cwd_subdir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/app/execute_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/app/nbextensions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/app/no_kernelspec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/app/no_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/app/non_existing_kernel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/notebooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/preheat_configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/app/preheat_multiple_notebooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/app/serve_directory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/show_traceback_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down
4 changes: 1 addition & 3 deletions tests/app/template_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
2 changes: 1 addition & 1 deletion tests/app/tree_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/server/cwd_subdir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/server/execute_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/server/tree_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
27 changes: 13 additions & 14 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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,
),
Expand Down Expand Up @@ -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,
)
)
Expand Down Expand Up @@ -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,
),
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions voila/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion voila/notebook_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
2 changes: 1 addition & 1 deletion voila/server_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
4 changes: 2 additions & 2 deletions voila/tornado/contentshandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
2 changes: 1 addition & 1 deletion voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading