简体   繁体   English

有没有更简单的方法来制作内容菜单的 jupyter 笔记本内部链接? 因为他们似乎很难写

[英]Is there an easier way to make jupyter notebooks internal links for menu of content? as they seem laborious to write

The code to make a link for a menu of content is:为内容菜单创建链接的代码是:

<a href="#Section-1">Section 1</a>

In the first part of the tag, the spaces of the title have to be replaced by dashes '-'.在标签的第一部分,标题的空格必须用破折号“-”代替。 The second part just the normal text with spaces between words.第二部分只是在单词之间有空格的普通文本。

I have tried using:我试过使用:

string.replace(" ", "-")

to replace the spaces with dashes, but when I execute this on the cell, the string comes with ''.用破折号替换空格,但是当我在单元格上执行此操作时,字符串带有''。

Any ideas?有任何想法吗? Thanks in advance.提前致谢。

If you try using the print() function, it will print the string saved in your variable without the quotes:如果您尝试使用print() function,它将打印保存在变量中的字符串,不带引号:

print(your_string)

However, I think a better way would be to use input for the text, save it to a variable, use the replace() function, and the .format method.但是,我认为更好的方法是使用文本输入,将其保存到变量中,使用replace() function 和.format方法。

If you are looking just to copy this and paste it on another cell, try the following:如果您只是想复制它并将其粘贴到另一个单元格上,请尝试以下操作:



import pyperclip as pc
string = input('Enter your string:')
dashed_str = string.replace(" ", "-")
link = ('<a href="#{}">{}</a>'.format(dashed_str, string,))
pc.copy(link)

I would suggest you to install and import the pyperclip library to be able to save the string to the clipboard, instead of printing it then copying.我建议您安装并导入 pyperclip 库,以便能够将字符串保存到剪贴板,而不是打印然后复制。 Less click to do.少点击做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM