Scilab Plots in Browser

186 views
Skip to first unread message

Saket Choudhary

unread,
Sep 2, 2011, 1:42:47 PM9/2/11
to sage-n...@googlegroups.com
Hi !

I am currently developing a modified version of SAGE for my personal use so that I get to see the Scilab Plots in the browser itself while plotting from Sage notebook in Scilab mode.

My solutions so far include doing a grep for 'plot' and replacing it with necessary  code to save a plot on the Server side.
This also involved changing  the way scilab is called from scilab.pyfrom 'scilab -nogui' to 'scilab-adv-cli' OR 'scilab -nw'

My next target is to make an AJAX call and fetch the saved image from the server and render it inside the notebook inside the browser itself ( Something that happens with matplotlib when using python in the notebook)


I would first want to know the way it works for matplotlib . The  notebook_lib.js is I believe the request sender and receiver .

What should be done to fetch an image from the server ?


Regards
Saket

Jason Grout

unread,
Sep 2, 2011, 1:55:06 PM9/2/11
to sage-n...@googlegroups.com
On 9/2/11 12:42 PM, Saket Choudhary wrote:
> Hi !
>
> I am currently developing a modified version of SAGE for my personal use
> so that I get to see the Scilab Plots in the browser itself while
> plotting from Sage notebook in Scilab mode.
>
> My solutions so far include doing a grep for 'plot' and replacing it
> with necessary code to save a plot on the Server side.
> This also involved changing the way scilab is called from *scilab.pyfrom
> 'scilab -nogui' to 'scilab-adv-cli' OR 'scilab -nw'*
> *
> *

> My next target is to make an AJAX call and fetch the saved image from
> the server and render it inside the notebook inside the browser itself (
> Something that happens with matplotlib when using python in the notebook)
>
>
> I would first want to know the way it works for matplotlib . The
> *notebook_lib.js *is I believe the request sender and receiver .

>
> What should be done to fetch an image from the server ?

Currently what basically happens (IIRC) is that the image is saved to
disk in the temporary execution directory (in
sage.plot.plot.Graphics.save()). After the computation is done, the
notebook process looks through the files that were saved and sends links
to the browser (in sagenb.notebook.cell.Cell.files_html()). For
example, if it finds a jpg or png file, it sends an <img> tag to the
browser. The browser then requests the actual png file. If the file
hasn't been copied to the worksheet directory yet, then a symbolic link
is made to the temporary execution directory. At any rate, the notebook
server returns the actual image file.

Does that help? I think you would need to just get scilab to save the
image into the temporary execution directory and then Sage itself would
handle rendering the image, since Sage doesn't care who generated the
image. It just displays whatever images are in the temporary execution
directory.

Thanks,

Jason

kcrisman

unread,
Sep 2, 2011, 3:43:01 PM9/2/11
to sage-notebook
That's correct. I think that if you looked at what changed for e.g.
http://trac.sagemath.org/sage_trac/ticket/7665 (for instance, adding a
chdir command in the interface for R), that could help figure out how
to do this. From looking at sage.interfaces.scilab?? it looks like
you might need to add a few things to support this, unsure.

- kcrisman

Saket Choudhary

unread,
Sep 2, 2011, 4:54:24 PM9/2/11
to sage-n...@googlegroups.com
@jason
How do you access the SAGE_TMP variable in scilab ? I am currently writing a test code  like this

x=[0:0.5;3.14];
y=sin(x);
scf(0); 
plot2d (x,y); 
xs2jpg(0,'test.jpg');

the 'test.jpg' needs to be changed to SAGE_TMP + 'test.jpg' in case I need to go the way as suggested by you.

I am currently doing this as a proof of concept and hence the code above just needs a change in "scilab.py" from "scilab -nogui" to "scilab -nw". 

Jason Grout

unread,
Sep 2, 2011, 7:35:39 PM9/2/11
to sage-n...@googlegroups.com
On 9/2/11 3:54 PM, Saket Choudhary wrote:
> @jason
> How do you access the SAGE_TMP variable in scilab ? I am currently
> writing a test code like this
>
> x=[0:0.5;3.14];
> y=sin(x);
> scf(0);
> plot2d (x,y);
> xs2jpg(0,'test.jpg');
>
> the 'test.jpg' needs to be changed to SAGE_TMP + 'test.jpg' in case I
> need to go the way as suggested by you.
>
> I am currently doing this as a proof of concept and hence the code above
> just needs a change in *"scilab.py" from "scilab -nogui" to "scilab -nw". *

This is a scilab question; I don't know scilab. You just need to figure
out how to access environment variables from within scilab.

As for saving to the right temporary directory for the image to be
picked up in the notebook, that should be the current working directory
when the code is executing. So just saving to the current directory
should work...

Thanks,

Jason

Saket Choudhary

unread,
Sep 2, 2011, 8:54:31 PM9/2/11
to sage-n...@googlegroups.com
Saving in the current directory which is "sage/data/extcode/scilab/user " doesn' t work.
Any work arounds ? I save it with: "xs2jpg(0,string(pwd())+'/test.jpg');". Whats wrong ?

William Stein

unread,
Sep 3, 2011, 1:38:16 AM9/3/11
to sage-n...@googlegroups.com

Tell scilab to save in the directory that is output in that cell when
you type "os.getcwd()" in Sage. This is probably in
/tmp on your computer...

> Any work arounds ? I save it with: "xs2jpg(0,string(pwd())+'/test.jpg');".
> Whats wrong ?
>

--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org

Saket Choudhary

unread,
Sep 3, 2011, 6:07:25 AM9/3/11
to sage-n...@googlegroups.com
I am kind of still stuck badly. 
I am probably stuck because of my failure to understand where should I be storing the "test.jpg" file for it to be rendered in the browser after Scilab computation.

1. I run SAGE and then do a "os.getcwd()" . The output for the follwoing is:
'/home/saket/Downloads/sage-4.7-linux-32bit-ubuntu_10.04_lts-i686-Linux'

2.I hardcode my Scilab code to save the image as follows in the SAGE notebook:

x=[0:0.5;3.14];
y=sin(x);
scf(0); 
plot2d (x,y); 
xs2jpg(0,'/home/saket/Downloads/sage-4.7-linux-32bit-ubuntu_10.04_lts-i686-Linux/test.jpg')

The image is saved , but not rendered in the browser. Whats going wrong here? 
Am I saving it in the wrong directory?

Jason Grout

unread,
Sep 3, 2011, 8:31:08 AM9/3/11
to sage-n...@googlegroups.com
On 9/3/11 5:07 AM, Saket Choudhary wrote:
> I am kind of still stuck badly.
> I am probably stuck because of my failure to understand where should I
> be storing the "test.jpg" file for it to be rendered in the browser
> after Scilab computation.
>
> 1. I run SAGE and then do a "os.getcwd()" . The output for the follwoing is:
> *'/home/saket/Downloads/sage-4.7-linux-32bit-ubuntu_10.04_lts-i686-Linux'*
> *
> *

> 2.I hardcode my Scilab code to save the image as follows in the SAGE
> notebook:
>
> x=[0:0.5;3.14];
> y=sin(x);
> scf(0);
> plot2d (x,y);
> *xs2jpg(0,'/home/saket/Downloads/sage-4.7-linux-32bit-ubuntu_10.04_lts-i686-Linux/test.jpg')*

>
> The image is saved , but not rendered in the browser. Whats going wrong
> here?
> Am I saving it in the wrong directory?

William meant for you to run os.getcwd() in the notebook, probably in
the same cell, not in the command line before starting the noteobook.

when you do just:

xs2jpg(0,'test.jpg')

does the graphic not appear? I would think that that command would save
the graphic in the current working directory, which is exactly what you
want.

The pwd() function in scilab gets the current directory. If you put
print the result of pwd() in scilab, what directory does it give?

Thanks,

Jason

Saket Choudhary

unread,
Sep 3, 2011, 8:55:12 AM9/3/11
to sage-n...@googlegroups.com
I am sorry if this is getting troubling but I am a bit confused.
I did a "os.getcwd()" in the cell and everytime it gives me a different directory. That means everytime I run a code , irrespective of the code language a /tmp/tmpXXXX directory is created.

Now the main thing : Are the figures/graphs generated need to be stored in the /tmp/tmpXXXX directory ?

I tried saving it with xs2jpg(0,'test,jpg') the image doesn't show up for the first time, what happens is I get a error message like:

       
WARNING: Due to your configuration limitations, Scilab switched in a
mode where mixing uicontrols and graphics is not avail 
      able. Type "help usecanvas" for more information.


  which should not happen given I am calling with 'scilab-adv-cli -nb' . Is it the reason?

Also is their an equivalent in Scilab mode for "os.getcwd()" ?

Jason Grout

unread,
Sep 3, 2011, 2:27:21 PM9/3/11
to sage-n...@googlegroups.com
On 9/3/11 7:55 AM, Saket Choudhary wrote:
> I am sorry if this is getting troubling but I am a bit confused.
> I did a "os.getcwd()" in the cell and everytime it gives me a different
> directory. That means everytime I run a code , irrespective of the code
> language a /tmp/tmpXXXX directory is created.

>
> Now the main thing : Are the figures/graphs generated need to be stored
> in the /tmp/tmpXXXX directory ?

Yes. That is what I meant when I said that your code is run in a
temporary directory specific to that cell. Sage will copy any files
created (including images) from this temporary directory back into the
sage notebook worksheet directory after the cell finishes executing.

>
> I tried saving it with xs2jpg(0,'test,jpg') the image doesn't show up
> for the first time, what happens is I get a error message like:
>
>
>
> WARNING: Due to your configuration limitations, Scilab switched in a
> mode where mixing uicontrols and graphics is not avail
> able. Type"help usecanvas" for more information.
>

I have no clue what this error message is about or how to prevent it.
It sounds like the image isn't being generated, which would explain no
image showing up.


> which should not happen given I am calling with *'scilab-adv-cli -nb'
> . *Is it the reason?


>
> Also is their an equivalent in Scilab mode for "os.getcwd()" ?
>

That is the scilab pwd() command that I mentioned in my message (see below).

Thanks,

Jason

>
>
> On 3 September 2011 18:01, Jason Grout <jason...@creativetrax.com

> <mailto:jason...@creativetrax.com>> wrote:
>
> On 9/3/11 5:07 AM, Saket Choudhary wrote:
>
> I am kind of still stuck badly.
> I am probably stuck because of my failure to understand where
> should I
> be storing the "test.jpg" file for it to be rendered in the browser
> after Scilab computation.
>
> 1. I run SAGE and then do a "os.getcwd()" . The output for the
> follwoing is:

> *'/home/saket/Downloads/sage-__4.7-linux-32bit-ubuntu_10.04___lts-i686-Linux'*


> *
> *
> 2.I hardcode my Scilab code to save the image as follows in the SAGE
> notebook:
>
> x=[0:0.5;3.14];
> y=sin(x);
> scf(0);
> plot2d (x,y);

> *xs2jpg(0,'/home/saket/__Downloads/sage-4.7-linux-__32bit-ubuntu_10.04_lts-i686-__Linux/test.jpg')*

William Stein

unread,
Sep 3, 2011, 2:45:44 PM9/3/11
to sage-n...@googlegroups.com
Hi,

Each time the code in a cell is executed, the Python interpreter is
changed to a certain temporary directory. There are dozens of pexpect
interfaces, and it would be inefficient to *also* change their
directories to that same temporary directory.

There is an example of how to get the current Python directory and
output an image to it with the Mathematica interface. See the show()
function in

SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py

-- William

--

Saket Choudhary

unread,
Sep 5, 2011, 7:08:36 AM9/5/11
to sage-n...@googlegroups.com
So Should my strategy be to write a custom routine for  like "show()" for Scilab interface too.

If yes can you give an example how a sutom routine say "show()" if defined in scilab.py be called from the notebook.

I am a newbie to sage and currently trying to understand the whole flow of code and hence the chaos !

Saket Choudhary

unread,
Sep 13, 2011, 5:02:20 PM9/13/11
to sage-n...@googlegroups.com
Is it possible defining custome function in scilab.py and then calling it from the cell?
Reply all
Reply to author
Forward
0 new messages