hugetim / nbstata

A Jupyter kernel for Stata built on pystata
https://hugetim.github.io/nbstata/
GNU General Public License v3.0
26 stars 2 forks source link

Browse Functionality #6

Open cocopops17 opened 1 year ago

cocopops17 commented 1 year ago

Is it possible to get "browse" to work as it did with stata_kernel (i.e. open Stata's native browser in a new window)? I'm still using the Hydrogen package in Atom to run my code, so the currently-implemented %browse magic doesn't work for me.

hugetim commented 1 year ago

In short: it may be possible, but it looks to be pretty involved, if so.

Stata's pystata api doesn't allow for it. So we'd have to try the completely separate route of Stata Automation that stata_kernel uses--and I'm not sure that separate approach can be made to play nicely with pystata.

To confirm my understanding, are you using Windows, or MacOS? If Windows, you did that extra install step for stata_kernel, right?

Currently, I'm only vaguely familiar with how this part of stata_kernel works, so it would be a steep climb for me to attempt adding this functionality personally. I'd be quick to pull it in if someone else codes it, though.

Another possibility would be to petition StataCorp to add this functionality to pystata, but I'm not sure they will see that as in their interest.

I'd also really like to have this feature. In practice, I myself currently go back and forth between working in Jupyter Lab and the normal Stata application, largely to be able to use browse. As long as the data you're working with isn't too big, it is relatively quick to save a .dta file in the notebook and then open it in normal Stata. But it would be really nice to streamline that workflow further somehow.

%head and %tail should work for you, at least, though I recognize they are nowhere near the same. (Another imperfect substitute: there may be a way to get nbstata's %browse plug-in to work with Atom, maybe starting from here, but that's definitely beyond my own aspirations.)

gaksaray commented 1 year ago

This is also my biggest gripe with using Jupyter Notebooks for Stata with the current pystata API, which prevents me from fully embracing the workflow for exploratory analysis. @hugetim inspired by your call for petition, I posted this on Statalist.

In the meantime, I had experimented with various third-party data viewers for Python and I believe the best one is D-Tale. Would it ever be possible to have something like this integrated into %browse magic?

hugetim commented 1 year ago

Great, I'll go upvote you on Statalist!

And thanks for finding D-Tale! I think that looks better (and much easier to install) than the thing I had been using, so I expect I'll replace that with this very soon.

hugetim commented 1 year ago

Ok, I spent several hours trying to get D-tale to work reliably, and I couldn't. (And that's not even considering the difficulties campus folks would have trying to get it to work on JupyterHub.) Instead, I'm going with a much simpler and more robust option in ipydatagrid.

hugetim commented 1 year ago

But I'm thinking to add a %%python magic (someday) that would let you access dtale on your own if you want..

gaksaray commented 1 year ago

I tried the new %browse magic in v0.5.0 and I like the fact that it's plain and requires no other packages to be installed. However, I experience sluggishness on my MacBook M1 (Safari). Please see the screen capture. Is this normal? I'd expect it to be buttery smooth as it's seemingly a very simple applet.

Also, two minor questions: Would it be possible to have an option to move this to a separate window (as in Data Editor in Stata)? And is it possible to change the font within the view (from Courier to something else)?

https://user-images.githubusercontent.com/72267919/224432250-ed3becac-b825-474e-a6ed-109bf9a267c5.mov

hugetim commented 1 year ago

Right click on it in JupyterLab and select "Create New View for Output." That gives you a separate tab within JupyterLab. But if you meant moving it to a separate browser tab, I don't know if that's possible.

On the sluggishness, I wasn't able to replicate that, trying it just now, so I'm not sure what to suggest. There's no sluggishness for me (albeit on a powerful Windows desktop, using Chrome).

I'm reluctant to change the default font because that's deliberately matching Stata's default look. I could make it configurable if that's important to you though. I guess maybe it's easier to see long strings with a different font?

gaksaray commented 1 year ago

This is what I see on classic Notebook:

Screen Shot 2023-03-24 at 10 39 56 PM

There is no way to resize that browse window.

Then I switch to Lab. Datagrid looks fine, but the choppiness is real (mind you this is the tiny auto dataset):

https://user-images.githubusercontent.com/72267919/227656440-7fca20a7-b9fe-4c45-a5b5-47c2899a4841.mov

I'm on M1 Mac with macOS Monterey and Safari so I don't know if this is specific to Apple setup.

hugetim commented 1 year ago

As a workaround while we try to address those issues, you can use %head [N] or %tail [N] as an alternative to %browse (replacing [N] with a number at least as large as your observation count). That should give you output equivalent to pystata_kernel's %browse magic.