在plotly划应用程序,我增加一个文注释有可点击的链接,有一个散。
topic = "Australia" # might contain spaces
hashtag = "#" + topic
annotation_text=f"<a href=\"https://twitter.com/search?q={urllib.parse.quote_plus(hashtag)}&src=typed_query&f=live\">{topic}</a>"
我需要输出html包含 "https://twitter.com/search?q=%23Australia&src=typed_query&f=live"
但我不能获得"#"的字符编码正常。 它得到双编码%2523.
最小的工作,例如:
import dash
from dash.dependencies import Input, Output
import plotly.express as px
import urllib.parse
df = px.data.gapminder()
all_continents = df.continent.unique()
app = dash.Dash(__name__)
app.layout = dash.html.Div([
dash.dcc.Checklist(
id="checklist",
options=[{"label": x, "value": x}
for x in all_continents],
value=all_continents[4:],
labelStyle={'display': 'inline-block'}
),
dash.dcc.Graph(id="line-chart"),
])
@app.callback(
Output("line-chart", "figure"),
[Input("checklist", "value")])
def update_line_chart(continents):
mask = df.continent.isin(continents)
fig = px.line(df[mask],
x="year", y="lifeExp", color='country')
annotations = []
df_last_value = df[mask].sort_values(['country', 'year', ]).drop_duplicates('country', keep='last')
for topic, year, last_lifeExp_value in zip(df_last_value.country, df_last_value.year, df_last_value.lifeExp):
hashtag = "#" + topic
annotations.append(dict(xref='paper', x=0.95, y=last_lifeExp_value,
xanchor='left', yanchor='middle',
text=f"<a href=\"https://twitter.com/search?q={urllib.parse.quote_plus(hashtag)}&src=typed_query&f=live\">{topic}</a>",
# text=f"<a href=\"https://twitter.com/search?q=#{urllib.parse.quote_plus(topic)}&src=typed_query&f=live\">{topic}</a>",
font=dict(family='Arial',
size=16),
showarrow=False))
fig.update_layout(annotations=annotations)
return fig
app.run_server(debug=True)
当你跑这并点击的案文"澳大利亚"底线图,它应该打开了一个twitter搜索页面#澳大利亚。
我已经试过:
- 只是使用一个裸露的"#"character:
text=f"<a href=\"https://twitter.com/search?q=#{urllib.parse.quote_plus(topic)}&src=typed_query&f=live\">{topic}</a>"
在这里,排名字不是编码为%23在的输出,这导致一个断开链接。
https://twitter.com/search?q=#mytopic&src=typed_query&f=live
链接
- 使用quote_plus上的标签
text=f"<a href=\"https://twitter.com/search?q=#{urllib.parse.quote_plus(hashtag)}&src=typed_query&f=live\">{topic}</a>"
在这里,%23(编码#character)得到再次编码,导致%2523在的输出。
https://twitter.com/search?q=%2523mytopic&src=typed_query&f=live
链接
我怎么得到它的正确编码的#(到%23)所以我得到
href="https://twitter.com/search?q=%23mytopic&src=typed_query&f=live