ticoneva / pystata-kernel

Jupyter kernel for Stata based on pystata
GNU General Public License v3.0
13 stars 6 forks source link

Cells with more than one command echo back all the commands #4

Open ideabucket opened 2 years ago

ideabucket commented 2 years ago

Thanks so much for writing this!

If you have a cell that contains more than one command, the output from Stata contains the full console, including each command. For example, here's what you get if you run two commands from one cell:

sysuse auto
tab foreign rep78
. sysuse auto
(1978 automobile data)

. tab foreign rep78

           |                   Repair record 1978
Car origin |         1          2          3          4          5 |     Total
-----------+-------------------------------------------------------+----------
  Domestic |         2          8         27          9          2 |        48 
   Foreign |         0          0          3          9          9 |        21 
-----------+-------------------------------------------------------+----------
     Total |         2          8         30         18         11 |        69 

. 

But if you break up the commands to one per cell you don't get the echo:

sysuse auto
(1978 automobile data)
tab foreign rep78
           |                   Repair record 1978
Car origin |         1          2          3          4          5 |     Total
-----------+-------------------------------------------------------+----------
  Domestic |         2          8         27          9          2 |        48 
   Foreign |         0          0          3          9          9 |        21 
-----------+-------------------------------------------------------+----------
     Total |         2          8         30         18         11 |        69 

Based on the documentation for pystata.stata.run it looks like the way to fix this is to wrap what's passed as its code parameter in """, which will make Stata treat the cell like a do-file.

I'd write a PR for this but I don't know Python. Sorry!

ticoneva commented 2 years ago

I have added a new option echo. Setting it to True in the configuration file should give you the desired effect.

  1. Install the latest version:
    pip install git+https://github.com/ticoneva/pystata-kernel.git
  2. Create a configuration file during installation:
    python -m pystata-kernel.install --conf-file
  3. Set echo to True in the configuration file:
    [pystata-kernel]
    ...
    echo = True

    Let me know if it works or not.

ideabucket commented 2 years ago

Thanks for the quick response—this produces consistent behaviour, in that all cells now echo back the command, but it's the opposite of what I wanted. I'd like to suppress the echo in multi-command cells (which was the stata_kernel behaviour). In other words:

sysuse auto
tab foreign rep78
(1978 Automobile Data)

    Repair |
    Record |       Car type
      1978 |  Domestic    Foreign |     Total
-----------+----------------------+----------
         1 |         2          0 |         2 
         2 |         8          0 |         8 
         3 |        27          3 |        30 
         4 |         9          9 |        18 
         5 |         2          9 |        11 
-----------+----------------------+----------
     Total |        48         21 |        69 
ticoneva commented 2 years ago

Unfortunately, there appears to be no way to do that directly through pystata—or Stata, for that matter. The workaround is to use a combination of quietly and noisily:


quietly {
  noisily: sysuse auto
  noisily: tab foreign rep 78 
}
ticoneva commented 2 years ago

I spoke too soon. There is a way and it will be incorporated in the next version.

ticoneva commented 2 years ago

Version 0.1.20 accepts echo = None, which suppresses all commands.

ticoneva commented 2 years ago

Handling multi-line commands requires porting stata_kernel's CodeManager class with significant changes. Since this will take significant effort, I am removing this feature until it actually works.