【问题标题】:How to insert a variable in href link in iPython markup如何在 iPython 标记的 href 链接中插入变量
【发布时间】:2016-01-25 06:51:31
【问题描述】:

我正在使用 Ipython Notebook 2

我在变量中有一个文件名,它动态计算当前目录中文件的位置和文件名,我想将该变量插入到我的标记中以下载它

下面是代码:

  result_file ="Revenue_per_city"+start_date+"_"+end_date+".xlsx

然后是我下载文件的标记:

 <a href= {{result_file}}> Click here to download the result</a> 

但它没有按预期工作

【问题讨论】:

  • 有一个错字。代码中href的拼写。
  • @JasonEstibeiro 感谢您的更正,但这不是问题 :)

标签: python ipython ipython-notebook jupyter-notebook ipython-magic


【解决方案1】:

您可以使用 IPython.display 模块中的工具显示 HTML:

  • HTML 是将 HTML 表示为文本的对象
  • display 是一个显示对象的函数(如 print,但用于富媒体对象)

例如:

from IPython.display import display, HTML

# create a string template for the HTML snippet
link_t = "<a href='{href}'> Click here to download the result</a>"

result_file = "Revenue_per_city" + start_date + "_" + end_date + ".xlsx"

# create HTML object, using the string template
html = HTML(link_t.format(href=result_file))

# display the HTML object to put the link on the page:
display(html)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多