"Probably nothing big, but I can't manage it in Python."
Because you are using the exclamation point to issue a shell command via Jupyter/IPython, it is easier to do with abilities added by Jupyter/IPython without changing your code much.
You can use the Python variable flanked by brackets to pass in the value of variable to shell commands, like so in the context if your code:
# get filename of the current notebook
from requests import get
from socket import gethostname, gethostbyname
ip = gethostbyname(gethostname()) # 172.28.0.12
filename = get(f"http://{ip}:9000/api/sessions").json()[0]["name"]
print(filename)
# mount Drive to access data files
from google.colab import drive
drive.mount('./mount')
# print HTML file of the notebook next to the original file
!jupyter nbconvert --to html 'mount/My Drive/Colab Notebooks/{filename}'
More generalized to be able to test this in any Jupyter session:
# get filename of the current notebook
filename = "this_notebook_here.ipynb"
#print(filename)
# print HTML file of the notebook next to the original file
!jupyter nbconvert --to html 'mount/My Drive/Colab Notebooks/{filename}'
Running that you'll see it throws a warning as long as you have no notebook matching that file name, and if you inspect that you'll see it indeed has passed in the value set in Python to the shell command:
[NbConvertApp] WARNING | pattern 'mount/My Drive/Colab Notebooks/this_notebook_here.ipynb' matched no files
This application is used to convert notebook files (*.ipynb)
to various other formats.
I'm sure this is covered in a lot of places, here are some below. You'll see focusing on the specific command and not the general ability probably lead to you not seeing the answer in your searches. Additionally, Jupyter notebooks for Python developed from IPython notebooks, which had developed out of the IPython efforts and so some of the tricks such as these are out of that legacy and sometimes referencing those in your searches helps.
And if you really meant the 'in Python' at the end of your current post title ....
Current post title: 'How to replace a part of string by a variable in a nbconvert instruction in Python?'
And for completeness sake, you could actually restructure your shell command to use subprocess
(see here, here & here) and directly use the Python variable as part of that pure Python route to specifically get at the 'in Python' end of your post title, but it is overkill in this case because I suspect you didn't actually mean 'in Python'