TiesdeKok / ipystata

Enables the use of Stata together with Python via Jupyter (IPython) notebooks.
192 stars 68 forks source link

feature request #7

Open cschwem2er opened 8 years ago

cschwem2er commented 8 years ago

Is it possible to toggle cell magic on/off for multiple cells? This would be cool because we wouldn't have to use %%stata for every cell.

TiesdeKok commented 8 years ago

I have been thinking about that as well, it would be a very convenient feature for sure. I will keep it in the back of my mind, maybe I can figure out a way to add it!

cschwem2er commented 8 years ago

Do you think a notebook extension like RISE is necessary for this? Or could this be implemented in ipystata directly?

TiesdeKok commented 8 years ago

My limited research on this functionality does suggest that the best way would be to develop a notebook extension that allows the user to apply cell magic to a group of selected cells. The notebook 4.1 release adds multi-cells selection so I hope that someone with more knowledge of developing notebook extensions might be able to build on this and figure out a way to do it.

cschwem2er commented 8 years ago

I tried to contact an expert: https://github.com/Carreau/jupytercontrib/issues/1#issuecomment-204510308

mbaker21231 commented 8 years ago

Hello --

Another neat feature might be to have cell magic for mata. It's not a big deal, but instead of typing something like:

%%stata -s ses1
mata:
<mata code>
end

It would be nice to just be able to type

%%mata

This actually has a practical purpose - if one forgets to put the end in the cell, it runs forever. I'm ashamed to admit that I forget this all the time!

Matt

TiesdeKok commented 8 years ago

Would something like a -m / --mata argument help? Changing it to %%mata is a bit more complex as that would make it a new magic class.

Having a --mata argument that automatically wraps the cell code with mata: and end should however be pretty easy to implement.

mbaker21231 commented 8 years ago

Ties --

What you suggest would be great. One thing to maybe also think about is the difference between starting a Mata session with mata: versus mata - I'm not exactly sure what the difference is, and I almost always use the colon, but I know the two are somehow different!

Matt

TiesdeKok commented 8 years ago

Hi Matt,

I have implemented the -m / --mata arguments and adjusted my log file processor to filter out the Mata commands. Could you test it for me to see whether it works with your code (I don't really use Mata that much) and see if you have any suggestions for improvement?

I have uploaded a development version to pip, you can install it using: pip install ipystata==0.3.3.dev1 (You might have to pip uninstall ipystata first)

Using it is simple, you run for example:

%%stata --mata (or -m)
(1,2)\(3,4)
mbaker21231 commented 8 years ago

Hi Ties --

First, thanks very much for doing all this! I've checked it out and it works great - and in retrospect, it's probably better to have a heading like stata --mata rather than just mata as it gives one a more accurate description as to what the cell is doing.

Thanks again!

Matt

TiesdeKok commented 8 years ago

Hi Matt,

Great, you are more than welcome! PyPi is having issues right now but I will push it as an official version somewhere this weekend. :smile:

mbaker21231 commented 8 years ago

This might already be possible, but I am curious if one can break lines within an ipystata cell, as one would use the /// characters in Stata.

TiesdeKok commented 8 years ago

This is not currently possible (see #9 for the reason why).

However, it is on my to-do list to write a pre-processor that will use Python to take the code contents of a cell and convert it into "valid" Stata code for the interactive window. This would allow the use of /// for line breaks (my code will essential interpret the /// and turn them into a single-line again before sending it to Stata, circumventing the problem).

I will try to look into it asap.

TiesdeKok commented 8 years ago

It actually turned out to be a very easy solution. As long as the /// is directly followed by a new line it should work now with version 0.3.5. You can update using pip install ipystata --upgrade --force-reinstall

mbaker21231 commented 8 years ago

Ties --

Wonderful! I'll give it a try as soon as I get a chance, and thanks for the update!

Matt