Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a metadata tag to override notebook direction (ltr/rtl) #5052

Merged
merged 2 commits into from
Jul 23, 2020

Conversation

m2-farzan
Copy link
Contributor

Currently, the direction of text flow in a notebook (left-to-right / right-to-left) is determined by browser language settings. If the browser is set to use a language with right-to-left script, all notebooks are displayed in RTL direction and vice versa.

This behavior could be improved, IMHO (I'm a student from the middle east). I write my homework in my mother language, but I also download stuff from the internet which are mostly in English. This means that I have to change my browser language settings frequently, which is not user friendly.

This PR makes it possible to override this default behavior per notebook using a metadata tag. Here's how to use this tag in order to make a notebook RTL:

"direction": "rtl"

or to force notebook to use LTR:

"direction": "ltr"

The end user could either manually set this tag or use the toggle rtl layout command from the command palette to change the direction of a notebook permanently, without needing to change the browser settings.

That's it from the end user point of view.

Comment on lines 81 to 95
try{
requirejs(['custom/custom'], function() {});
bidi.loadLocale();
} catch(err) {
console.log("Error processing custom.js. Logging and continuing");
console.warn(err);
}
Copy link
Contributor Author

@m2-farzan m2-farzan Nov 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important note:

I've moved these lines to the very end of the file, because the function bidi.loadLocale now reads metadata from the notebook and must be called after loading it. The problem is that I don't know if this change could cause any unwanted side effects, especially since there's a line that loads a custom script and I don't know what it is at all.

Please just make sure I'm not breaking things here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom.js is meant as a place for users to put arbitrary javascript to customise the notebook interface. Which probably means any change to how it's loaded is going to break something for someone, somewhere, but that can't really be helped.

I'm not sure why loading it is tied to bidi support, though. :-/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @m2-farzan, I suggest we break this up: leave the custom.js loading where it is and move the bidi.locale() handling to the end of the file. This PR also needs a rebase.

@takluyver
Copy link
Member

Thanks! There's some relevant discussion at ipython/ipython#10980 . Someone has suggested ignoring the browser locale settings and always using LTR layout by default. That would still be compatible with having a direction stored in the notebook metadata, I think.

(I can't read any RTL languages, so I'm hoping that a consensus will appear among people who can about the best ways to handle them)

@m2-farzan
Copy link
Contributor Author

So I've been working on this recently, and I hope you like the end result.

30ed681 will allow the users to control the direction of the notebook by the following means:

  • a notebook metadata tag, direction, which could be either ltror rtl

  • a cell metadata tag, again direction, that overrides notebook settings. Either rtl or ltr.
    (Why this one? A common example is that when writing in RTL languages, users often need to write latex equation blocks for which you need an LTR cell. So cell-level control is essential.)

The behavior is described below:

  • A notebook without a direction tag will be LTR. That is, notebook will ignore browser locale settings from now on. This behavior was suggested by two users in Jupyter notebook become RTL after upgrade ipython/ipython#10980. It also keeps a notebook from looking different on different computers. However, please note that we're breaking the previous behavior here.

  • A markdown cell without a direction tag will inherit direction from notebook. A direction tag overrides it. I've also added an action in the command palette "toggle current cell ltr/rtl layout direction" which sets the tag for the user. (How about a shortcut for it?)

  • In a code cell, the code itself (code mirror) will never become RTL—no one ever writes code in RTL mode. However, a direction tag with value rtl will cause the output to become RTL. The code cell always defaults in LTR (that is, it doesn't inherit from notebook settings), because IMHO RTL output from code is rarely required and it would be a pain change it back every time. Most people are just dealing with math or getting simple English logs/errors.

Copy link
Contributor

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Marking this for 6.2 since we're in a 6.1 release cycle.

@blink1073
Copy link
Contributor

I kicked the CI jobs

@blink1073 blink1073 modified the milestones: 6.1, 6.2 Jun 22, 2020
@blink1073
Copy link
Contributor

Thanks for fixing the CI!

@kevin-bates kevin-bates mentioned this pull request Jul 22, 2020
24 tasks
@blink1073 blink1073 modified the milestones: 6.2, 6.1 Jul 23, 2020
@blink1073 blink1073 merged commit 9de5042 into jupyter:master Jul 23, 2020
@zareami10
Copy link

It would be great if we could also have this in Lab.

@m2-farzan
Copy link
Contributor Author

@zareami10 I've been working on it recently. It will be ready soon.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants