Plotly หมายเหตุประกอบ annotation tool ข้อความ:การเข้ารหัสอักขระที่ค่อยจัดการเรื่อง(#)ตัวอยู่ในที่อยู่ URL

0

คำถาม

ใน plotly วิ่งแอพฉันเพิ่มเป็นข้อความหมายเหตุประกอบ annotation tool กับ clickable เชื่อมต่อนั้นมีค่อยจัดการเรื่องอยู่ในนั้น

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" แต่ฉันไม่สามารถได้"#"ตัวละครเพื่อทำการเข้ารหัสอย่างเหมาะสม มันจะส encoded ไปยัง%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)

ตอนที่คุณวิ่งนี้แล้วคลิกที่ปุ่มข้อความ"ออสเตรเลีย"ตอนจบของเส้นกราฟ,มันควรจะเปิดขึ้นมาเป็นทวิตเตอร์ค้นหาหน้าสำหรับ#ออสเตรเลีย


สิ่งที่ฉันพยายาม:

  1. แค่ใช้เปลือย"#"อักขระ: text=f"<a href=\"https://twitter.com/search?q=#{urllib.parse.quote_plus(topic)}&src=typed_query&f=live\">{topic}</a>"

ที่นี่#ตัวละครยังไม่ encoded เป็น%23 ในการส่งออกซึ่งผลตรวจในหักเชื่อมโยงเพื่อนของทวิตเตอร์

https://twitter.com/search?q=#mytopic&amp;src=typed_query&amp;f=live เชื่อมโยง

  1. ใช้ quote_plus อยู่ด้วยแท็ก text=f"<a href=\"https://twitter.com/search?q=#{urllib.parse.quote_plus(hashtag)}&src=typed_query&f=live\">{topic}</a>"

ที่นี่%23(การเข้ารหัส#คน)จะได้ encoded อีกครั้งผลลัพธ์จากใน%2523 อยู่ในส่วนส่งออก

https://twitter.com/search?q=%2523mytopic&amp;src=typed_query&amp;f=liveเชื่อมโยง


แล้วฉันจะทำยังไงให้ได้มันต้องอย่างถูกต้องทำการเข้ารหัสที่#(เป็น%23)ดังนั้นฉันเข้าใจ

href="https://twitter.com/search?q=%23mytopic&amp;src=typed_query&amp;f=live

flask plotly plotly-dash python
2021-11-24 04:05:26
1

คำตอบที่ดีที่สุด

1

มันเป็นที่รู้จักแมลง: plotly/plotly.js#4084

Offending สายอยู่ plotly.js:

nodeSpec.href = encodeURI(decodeURI(href));
  • decodeURI ไม่ได้ถอดรหัส %23 (decodeURIComponent มัน).
  • encodeURI ไม่ได้ทำการเข้ารหัส # แต่ encodes % (encodeURIComponent มันทั้งสอง).

มากกว่านั้น: อะไรคือความแตกต่างระหว่าง decodeURIComponent และ decodeURI?

Workaround

คุณสามารถใช้แทนที่รองรับอยู่ภายใน encodeURI ต้องกลับไปใช้ค่าการเข้ารหัสอักขระของ % ใน %23:

app._inline_scripts.append('''
_encodeURI = encodeURI;
encodeURI = uri => _encodeURI(uri).replace('%2523', '%23');
''')
2021-12-03 07:04:19

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่