Plotly एनोटेशन पाठ: एन्कोडिंग हैश (#) चरित्र में एक URL

0

सवाल

में एक 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)

जब आप इस चलाने के लिए और पाठ पर क्लिक करें "ऑस्ट्रेलिया" के अंत में, लाइन ग्राफ, यह खुला होना चाहिए, एक चहचहाना खोज पृष्ठ के लिए #ऑस्ट्रेलिया.


क्या मैंने कोशिश की है:

  1. बस का उपयोग कर एक नंगे "#" चरित्र: 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&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 (इनकोडिंग # चरित्र) हो जाता है इनकोडिंग फिर, जिसके परिणामस्वरूप %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.जे एस#4084

हमलावर लाइन में plotly.js:

nodeSpec.href = encodeURI(decodeURI(href));
  • decodeURI नहीं समझाना %23 (decodeURIComponent करता है).
  • encodeURI नहीं करता है, सांकेतिक शब्दों में बदलना # लेकिन encodes % (encodeURIComponent दोनों करता है).

उस पर और अधिक: के बीच अंतर क्या है decodeURIComponent और decodeURI?

समाधान

आप ओवरराइड कर सकते हैं में निर्मित 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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में