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

Is there a way to have q syntax highlighting in cells starting with %%q in jupyter notebook? #149

Open
flcong opened this issue Jul 2, 2023 · 2 comments

Comments

@flcong
Copy link

flcong commented Jul 2, 2023

When using PyQ with python in jupyter notebook, all cells use syntax highlighting of Python by default. This makes q code written in cells starting with %%q very confusing (suppose you use ' somewhere, and all code following it is highlighted as string in python). Hence, I wonder if someone knows some way to use q syntax highlighting in cells starting with %%q.

I find a similar question on stackoverflow. There is an answer about SQL. They say adding the following code in ~/.jupyter/custom/custom.js to use SQL syntax highlighting in cells starting with %%sql.

require(['notebook/js/codecell'], function(codecell) {
  codecell.CodeCell.options_default.highlight_modes['magic_text/x-mssql'] = {'reg':[/^%%sql/]} ;
  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
  Jupyter.notebook.get_cells().map(function(cell){
      if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
  });
});

Yet, I am not sure how to modify the code in the answer so it recognizes q, particularly what the highlight_modes should be used.

@flcong
Copy link
Author

flcong commented Jul 3, 2023

After some experiments, I'm able to achieve the functionality using the following JS code:

require(['notebook/js/codecell'], function(codecell) {
  codecell.CodeCell.options_default.highlight_modes['magic_text/x-q'] = {'reg':[/^%%q/]} ;
  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){
  Jupyter.notebook.get_cells().map(function(cell){
      if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;
  });
});

This can be added either in ~/.jupyter/custom/custom.js or in a cell started with magic %%javascript. However, it seems that %%javascript cannot be used in Jupyter Lab. The mode name magic_text/x-q comes from jupyterq.

@sashkab
Copy link
Contributor

sashkab commented Jul 3, 2023

If I recall correctly, you need to install pygments-q module. I recall there was one busted release of pygments-q, so if 0.5 doesn't work for you, please install version 0.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants