"Problem with rendering languages in right-to-left direction on Plotly charts when using fig.write_image to save as a JPG

I am experiencing an issue when trying to export a chart to jpg in languages that are right-to-left direction, such as Arabic, Hebrew, and Urdu. When using the command "plotly.offline.plot" to export to .html, there is no problem and all brackets display correctly. However, when using the command "fig.write_image" to export to a jpg image, the brackets appear messy.

The type of chart I am working with is a sunburst chart.

Here is the HTML version for reference:

https://i.stack.imgur.com/hqKu7.png

And here is the JPG version showing the issue:

https://i.stack.imgur.com/HnrLm.png

The data being used includes the following entries:

الأخ (الأقدم)

بھائی (سب سے پرانا)

אח (המבוגר ביותר)

My question is whether this is a known issue or if there is a specific property that needs to be set to address it.

Answer №1

write_image() is designed to render RTL text in an LTR context, which means that mixed script strings or strings with punctuation may not display properly.

To work around this rendering issue, one solution is to wrap the strings in bidirectional formatting characters. I have implemented a custom module for this purpose, which encapsulates each string with the characters U+202B (RIGHT-TO-LEFT EMBEDDING) and U+202C (POP DIRECTIONAL FORMATTING).

An example in Sorani Kurdish:

https://github.com/enabling-languages/python-i18n/blob/main/notebooks/plotly2.ipynb

The strings are embedded within an LTR environment, while the formatting characters create an RTL environment within the LTR context.

We opted for embedding formatting characters instead of isolate formatting characters because kaleido does not handle isolate formatting characters correctly.

For instance:

def bidi_envelope(text, dir="rtl"):
    text = f"\u202A{text}\u202C" if dir.lower() =="ltr" else f"\u202B{text}\u202C"
    return text

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging the power of bash to execute a python script

I wrote a python script that takes a .txt file as input and produces another .txt file as output. My goal is to create a bash script that I can simply click on from my desktop to run the python script. This is what I have done so far: #!/bin/bash cd /Des ...

Having trouble locating a webpage using BeautifulSoup based on its URL

Currently, I am utilizing Python along with Selenium for the purpose of extracting all links from the results page of a particular search site. Regardless of what term I enter on the previous screen, the URL for any search conducted on the results page rem ...

What is the method to extract div text using Python and Selenium?

Is there a way to extract the text "950" from a div that does not have an ID or Class using Python Selenium? <div class="player-hover-box" style="display: none;"> <div class="ps-price-hover"> <div> ...

Using a user-defined object as a specified parameter type in Python functions

After defining my `Player` class in Python3, I found myself struggling to create a method in another class that specifically takes an object of type `Player` as a parameter. Here is what my code looks like: def change_player_activity(self, player: Player) ...

Tips for executing a Python function from JavaScript, receiving input from an HTML text box

Currently, I am facing an issue with passing input from an HTML text box to a JavaScript variable. Once the input is stored in the JavaScript variable, it needs to be passed to a Python function for execution. Can someone provide assistance with this pro ...

Learn how to retrieve values from a .json file in real-time and then perform comparisons with user input using Python

I have a JSON file structured like this: [ { "name": { "common": "Aruba", "official": "Aruba", "native": { "nld": { "official ...

Discovering the URL of an img tag within a CSV file column containing numerous links and then cross-referencing that link with another CSV file

import csv # Open the Topic or Reply file for reading csvfile = open('rad.csv', newline='') reader = csv.reader(csvfile) csvfile1 = open('new.csv', newline='') reader1 = csv.reader(csvfile1) # Extract image sources f ...

Kivy: Implementing a loading animation to prevent freezing during lengthy operations

These are the issues I am facing: My application freezes during a lengthy function If the user clicks multiple times while it's loading, it triggers multiple calls The visual display makes it appear as if there is a bug ...

Is there a way to extract a specific substring from a pandas dataframe using a provided list for filtering?

While I know this question has been asked before, I'm struggling with list comprehensions and my code has a small twist to it. In my dataframe, I have keywords that I want to filter based on whether they contain any of the keywords from a specific li ...

Struggling to find a specific element on a website with Xpath in Selenium using Python

Attempting to retrieve data from a website named: , specifically targeting the element: /html/body/div[1]/div/main/div/div[2]/div/div[2]/div/div/div[1]/div[4]/div[2]/div[1]/div[1]/div[2]/span[2] which displays volume as a number on the webpage. The code I ...

Using Python and Selenium to interact with an image button element by clicking on it

I've been attempting to click on that button, but no matter what I try, I can't seem to make it work. button id = edita_customer Here is the HTML code snippet: <div class="card mb-3" style="color: #ace;"> <div c ...

Is there an alternative method to fetching contents from Django Template loader as "get_contents()" function is not implemented in it?

Currently, I'm in the process of updating some older Django code, but I've hit a roadblock with the deprecated "get_contents()" function within the loader. I'm uncertain about how to move forward. Should I create a new child class from the l ...

The error message "No module named Image" is indicating that there is a missing module in the specified file path build/exe.win-amd64-3.6/scipy/misc

I am working on a Python application for image processing that will be compiled into an EXE file. My tools of choice are tkinter and cx_Freeze. However, I have encountered an error during the process. https://i.stack.imgur.com/ThRs4.jpg Below is the con ...

Removing file extensions using Selenium in Python

After utilizing a code snippet for proxy from this source, I encountered difficulties in disabling or removing the proxy extension. Despite my efforts to locate it in the extensions folder within the profile directory, no extensions were found installed vi ...

Having difficulty printing a browser.find_element_by_xpath using Selenium in Python

I am trying to use the following code: print browser.find_element_by_xpath('/html/body/div[2]/div/div[3]/div/div/div[1]/div[2]/ul/li[4]/ul/li/span[3]/span[3]').text However, I keep getting an unexpected token error on the "browser.find_element_ ...

What is the process for retrieving a player's data from the Statistics webpage using HTML code?

I am currently using selenium to scrape data from a website. Here's the link to the website I am working on: . The specific information I am trying to extract is located under the player's 'statistics' section. My current code opens the ...

The spacing in the pine tree program is not formatted correctly

I'm currently working on a project that involves drawing a pine tree, but I'm having trouble with the spacing. Here is the code snippet: def print_shifted_triangle(n, m, symbol): p1 = " " p2 = symbol number_of_asterisks = 1 numbe ...

Once all functions have been completed, Selenium will autonomously shut down the browser

Having recently started programming, I have a question about Selenium. I am confused as to why the browsers opened by Selenium close at the end of the code. from lib2to3.pgen2 import driver from selenium import webdriver def Online_PLatform(): ...

Executing Logo Button Click with Selenium in Python

When I try to click on a logo button after completing some downloads, it doesn't work and throws the following exception: NoElementException: unable to locate element: {"method":"css selector","selector":"brand.brand-bv"} The structure of the elemen ...

What is the best way to identify the coordinates of intricate RTL-SDR signal peaks?

I am successfully receiving signals from a RTL-SDR using the code snippet below: from rtlsdr import * i = 0 signals = [] sdr = RtlSdr() sdr.sample_rate = 2.8e6 sdr.center_freq = 434.42e6 sdr.gain = 25 while True: samples = sdr.read_samples(102 ...