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
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self):
if strict or missing:
log.warn('rebuilding js and css failed')
if missing:
log.error('missing files: %s' % missing)
log.error(f'missing files: {missing}')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
raise e
else:
log.warn('rebuilding js and css failed (not a problem)')
Expand Down Expand Up @@ -103,9 +103,9 @@ def run(cmd, **kwargs):
cmd = shlex.split(cmd)
cmd_path = shutil.which(cmd[0])
if not cmd_path:
sys.exit("Aborting. Could not find cmd (%s) in path. "
sys.exit(f"Aborting. Could not find cmd ({cmd[0]}) in path. "
"If command is not expected to be in user's path, "
"use an absolute path." % cmd[0])
"use an absolute path.")
cmd[0] = cmd_path
return check_call(cmd, **kwargs)

Expand Down Expand Up @@ -159,7 +159,7 @@ def run(self):

for t in self.targets:
if not os.path.exists(t):
msg = 'Missing file: %s' % t
msg = f'Missing file: {t}'
if not has_npm:
msg += '\nnpm is required to build a development version'
raise ValueError(msg)
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 @@ -10,7 +10,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
4 changes: 2 additions & 2 deletions tests/app/cwd_subdir_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,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"
jtpio marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + voila_args_extra


async def test_hello_world(http_server_client, cwd_subdir_notebook_url):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/execute_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@pytest.fixture
def cpp_file_url(base_url):
return base_url + "voila/render/print.xcpp"
return f"{base_url}/voila/render/print.xcpp"
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
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 @@ -19,7 +19,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}]'


async def test_lists_extension(http_server_client, base_url):
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 @@ -3,12 +3,12 @@

@pytest.fixture
def non_existing_kernel_notebook(base_url):
return base_url + "voila/render/no_kernelspec.ipynb"
return f"{base_url}/voila/render/no_kernelspec.ipynb"
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


@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
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 @@ -3,12 +3,12 @@

@pytest.fixture
def non_existing_kernel_notebook(base_url):
return base_url + "voila/render/non_existing_kernel.ipynb"
return f"{base_url}/voila/render/non_existing_kernel.ipynb"
jtpio marked this conversation as resolved.
Show resolved Hide resolved


@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
4 changes: 2 additions & 2 deletions tests/app/notebooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + voila_args_extra


async def test_other_comms(http_server_client, base_url):
response = await http_server_client.fetch(base_url + 'voila/render/other_comms.ipynb')
response = await http_server_client.fetch(f'{base_url}/voila/render/other_comms.ipynb')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
html_text = response.body.decode('utf-8')
assert 'This notebook executed' in html_text
2 changes: 1 addition & 1 deletion tests/app/serve_directory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + voila_args_extra


async def test_print(http_server_client, print_notebook_url):
Expand Down
2 changes: 1 addition & 1 deletion tests/app/template_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def voila_args_extra():
path_test_template = os.path.abspath(os.path.join(BASE_DIR, '../test_template/share/jupyter/voila/templates/test_template/'))
path_default = os.path.abspath(os.path.join(BASE_DIR, '../../share/jupyter/voila/templates/default'))
return ['--template=None', '--VoilaTest.template_paths=[%r, %r]' % (path_test_template, path_default), '--VoilaExecutor.timeout=240']
return ['--template=None', f'--VoilaTest.template_paths=[{path_test_template, path_default}, {'--VoilaExecutor.timeout=240'}]'
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


async def test_template_test(http_server_client, base_url):
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 @@ -4,7 +4,7 @@

@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + 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 @@ -14,7 +14,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"
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


@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"
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
def voila_args(notebook_directory, voila_args_extra):
return ['--VoilaTest.root_dir=%r' % notebook_directory, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + 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 @@ -6,7 +6,7 @@

@pytest.fixture
def cpp_file_url(base_url):
return base_url + "voila/render/print.xcpp"
return f"{base_url}/voila/render/print.xcpp"
cmaureir marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture
Expand All @@ -16,7 +16,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(not TEST_XEUS_CLING, reason='opt in to avoid having to install xeus-cling')
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, '--VoilaTest.log_level=DEBUG'] + voila_args_extra
return [f'--VoilaTest.root_dir={notebook_directory!r}', '--VoilaTest.log_level=DEBUG'] + voila_args_extra


@pytest.fixture
Expand All @@ -13,7 +13,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
28 changes: 14 additions & 14 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def display_url(self):
url += '/'
else:
if self.ip in ('', '0.0.0.0'):
ip = "%s" % socket.gethostname()
ip = f"{socket.gethostname()}"
else:
ip = self.ip
url = self._url(ip)
Expand All @@ -260,7 +260,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(),
Expand All @@ -285,7 +285,7 @@ def _config_file_paths_default(self):
@default('connection_dir_root')
def _default_connection_dir(self):
connection_dir = tempfile.gettempdir()
self.log.info('Using %s to store connection files' % connection_dir)
self.log.info(f'Using {connection_dir} to store connection files')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
return connection_dir

@default('log_level')
Expand Down Expand Up @@ -314,7 +314,7 @@ def _default_root_dir(self):
return getcwd()

def initialize(self, argv=None):
self.log.debug("Searching path %s for config files", self.config_file_paths)
self.log.debug(f"Searching path {self.config_file_paths} for config files")
# to make config_file_paths settable via cmd line, we first need to parse it
super(Voila, self).initialize(argv)
if len(self.extra_args) == 1:
Expand All @@ -326,9 +326,9 @@ def initialize(self, argv=None):
elif os.path.isfile(arg):
self.notebook_path = arg
else:
raise ValueError('argument is neither a file nor a directory: %r' % arg)
raise ValueError(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 @@ -358,19 +358,19 @@ 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 _handle_signal_stop(self, sig, frame):
self.log.info('Handle signal %s.' % sig)
self.log.info(f'Handle signal {sig}.')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
self.ioloop.add_callback_from_signal(self.ioloop.stop)

def start(self):
self.connection_dir = tempfile.mkdtemp(
prefix='voila_',
dir=self.connection_dir_root
)
self.log.info('Storing connection files in %s.' % self.connection_dir)
self.log.info('Serving static files from %s.' % self.static_root)
self.log.info(f'Storing connection files in {self.connection_dir}.')
self.log.info(f'Serving static files from {self.static_root}.')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved

self.kernel_spec_manager = KernelSpecManager(
parent=self
Expand Down Expand Up @@ -424,8 +424,8 @@ def start(self):
handlers = []

handlers.extend([
(url_path_join(self.server_url, r'/api/kernels/%s' % _kernel_id_regex), KernelHandler),
(url_path_join(self.server_url, r'/api/kernels/%s/channels' % _kernel_id_regex), ZMQChannelsHandler),
(url_path_join(self.server_url, fr'/api/kernels/{_kernel_id_regex}'), KernelHandler),
(url_path_join(self.server_url, fr'/api/kernels/{_kernel_id_regex}/channels'), ZMQChannelsHandler),
(
url_path_join(self.server_url, r'/voila/static/(.*)'),
MultiStaticFileHandler,
Expand Down Expand Up @@ -475,7 +475,7 @@ def start(self):
}
))
else:
self.log.debug('serving directory: %r', self.root_dir)
self.log.debug(f'serving directory: {self.root_dir!r})
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
handlers.extend([
(self.server_url, VoilaTreeHandler, tree_handler_conf),
(url_path_join(self.server_url, r'/voila/tree' + path_regex),
Expand Down Expand Up @@ -547,7 +547,7 @@ def launch_browser(self):
try:
browser = webbrowser.get(self.browser or None)
except webbrowser.Error as e:
self.log.warning(_('No web browser found: %s.') % e)
self.log.warning(_(f'No web browser found: {e}.')
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
browser = None

if not browser:
Expand Down
4 changes: 2 additions & 2 deletions voila/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def strip_code_cell_errors(self, cell):

error_outputs = [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 = 'There was an error when executing cell [{cell["execution_count"]}]. {self.cell_error_instruction}'
cmaureir marked this conversation as resolved.
Show resolved Hide resolved

for output in error_outputs:
output['ename'] = 'ExecutionError'
Expand All @@ -121,7 +121,7 @@ def strip_code_cell_errors(self, cell):
def show_code_cell_timeout(self, cell):
"""Show a timeout error output in a code cell."""

timeout_message = 'Cell execution timed out, aborting notebook execution. {}'.format(self.cell_timeout_instruction)
timeout_message = f'Cell execution timed out, aborting notebook execution. {self.cell_timeout_instruction}'

output = {'output_type': 'error',
'ename': 'TimeoutError',
Expand Down
2 changes: 1 addition & 1 deletion voila/treehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def allowed_content(content):
url = url_path_join(
self.base_url, service, url_escape(path),
)
self.log.debug("Redirecting %s to %s", self.request.path, url)
self.log.debug(f"Redirecting {self.request.path} to {url}")
cmaureir marked this conversation as resolved.
Show resolved Hide resolved
self.redirect(url)
else:
raise web.HTTPError(404)
2 changes: 1 addition & 1 deletion voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ 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