How to export notebook worksheet to text file

181 views
Skip to first unread message

David Sanders

unread,
Jul 7, 2010, 5:20:42 PM7/7/10
to sage-support
Hi,

[Using 'Sage Version 4.4.4, Release Date: 2010-06-23' on Kubuntu
10.04.]

The following seems like it must come up a lot, but I have not been
able to find an answer (apologies if I missed it somewhere).

I built a worksheet with several cells in the notebook, and then
wanted to try to run it on a remote computer using the command line
interface. But I could not find a convincing way to export the
worksheet to a simple text file that I could import directly to sage,
i.e. a "something.sage" file.

In the end, I had to copy and paste each cell separately into a text
file on the remote machine, which I then imported with "load
something.sage" But this is clearly not a reasonable solution for a
long worksheet.

I tried the "Text" option in the worksheet, but that produced output
which I could not just copy straight into the sage command line, or
import with "load".

So what is the obvious solution that I am missing?

Thanks and best wishes,
David.

Justin C. Walker

unread,
Jul 7, 2010, 5:30:47 PM7/7/10
to sage-s...@googlegroups.com

On Jul 7, 2010, at 14:20 , David Sanders wrote:

> Hi,
>
> [Using 'Sage Version 4.4.4, Release Date: 2010-06-23' on Kubuntu
> 10.04.]
>
> The following seems like it must come up a lot, but I have not been
> able to find an answer (apologies if I missed it somewhere).
>
> I built a worksheet with several cells in the notebook, and then
> wanted to try to run it on a remote computer using the command line
> interface. But I could not find a convincing way to export the
> worksheet to a simple text file that I could import directly to sage,
> i.e. a "something.sage" file.

There's more to a worksheet than the sage commands.

You want to create a worksheet file (foo.sws), using "File->save
worksheet to a file..." (the 'File' menu on the worksheet). From
there you can "File->load worksheet from file".

HTH

Justin

--
Justin C. Walker
Curmudgeon at Large
Director
Institute for the Enhancement of the Director's Income
--
Build a man a fire and he'll be warm
for a night.
Set a man on fire and he'll be warm
for the rest of his life.

David Sanders

unread,
Jul 7, 2010, 5:40:34 PM7/7/10
to sage-support


> > [Using 'Sage Version 4.4.4, Release Date: 2010-06-23' on Kubuntu
> > 10.04.]
>
> > The following seems like it must come up a lot, but I have not been
> > able to find an answer (apologies if I missed it somewhere).
>
> > I built a worksheet with several cells in the notebook, and then
> > wanted to try to run it on a remote computer using the command line
> > interface.  But I could not find a convincing way to export the
> > worksheet to a simple text file that I could import directly to sage,
> > i.e. a "something.sage" file.

Hi and thanks for your reply.

>
> There's more to a worksheet than the sage commands.

OK, that's true, I should have been more specific.

>
> You want to create a worksheet file (foo.sws), using "File->save  
> worksheet to a file..." (the 'File' menu on the worksheet).  From  
> there you can "File->load worksheet from file".

That doesn't work for me, since I want to import from the command line
interface.
So, as far as I can tell, I can't import the binary .sws worksheet
file -- I need a plain Python script, or a "something.sage" file.

So my more precise question is:
How can I extract the sage commands from a worksheet into a plaintext
script which I can run from the command-line interface to sage (e.g.
on a remote machine)?
>
> HTH
>
> Justin
>
> --
> Justin C. Walker


Thanks,
David.

David Kirkby

unread,
Jul 7, 2010, 5:50:22 PM7/7/10
to sage-s...@googlegroups.com
On 7 July 2010 22:40, David Sanders <dpsa...@gmail.com> wrote:

> That doesn't work for me, since I want to import from the command line
> interface.
> So, as far as I can tell, I can't import the binary .sws worksheet
> file -- I need a plain Python script, or a "something.sage" file.
>
> So my more precise question is:
> How can I extract the sage commands from a worksheet into a plaintext
> script which I can run from the command-line interface to sage (e.g.
> on a remote machine)?

FWIW, Mathematica allows one to do what you want, so if Sage does not,
it would seem wise the functionality was added.

Dave

TianWei

unread,
Jul 7, 2010, 11:45:14 PM7/7/10
to sage-support
> I built a worksheet with several cells in the notebook, and then
> wanted to try to run it on a remote computer using the command line
> interface. But I could not find a convincing way to export the
> worksheet to a simple text file that I could import directly to sage,
> i.e. a "something.sage" file.

> In the end, I had to copy and paste each cell separately into a text
> file on the remote machine, which I then imported with "load
> something.sage" But this is clearly not a reasonable solution for a
> long worksheet.

> I tried the "Text" option in the worksheet, but that produced output
> which I could not just copy straight into the sage command line, or
> import with "load".

If Sage has an easy, automatic way of doing this, I'm not aware of it.
However, there's a couple ways to accomplish what you want to do
without doing a bunch of repetitive actions (for example, manually
copying out each the text cell). Unfortunately, both of my suggestions
require a bit of work:

(1) Open the worksheet through the notebook interface, then click on
the "Text" tab. Copy the text there, then write a little script to
strip out every line that doesn't begin with "sage: ". The lines that
have "sage: " prepended are lines in the input cells (assuming you
didn't type "sage: " somewhere in the worksheet itself). Strip out the
"sage: " text.

(2) You could tinker around with Notebook, Worksheet, and Cell objects
in Sage (using a script, command-line, or notebook interface). In
summary, you could create a Notebook object; get the desired Worksheet
object using one of the methods of the Notebook object; get a list of
Cell objects from the Worksheet; then pull out the input text from
each Cell object. The documentation for these objects are:

http://www.sagemath.org/doc/reference/sagenb/notebook/notebook.html
http://www.sagemath.org/doc/reference/sagenb/notebook/worksheet.html
http://www.sagemath.org/doc/reference/sagenb/notebook/cell.html

Again, I'm (obviously) not aware of a cleaner way to just pull out the
input text from a worksheet, and these two suggestions are just the
work-arounds I could think of.

As a side-note, the inverse process (converting from plain-text
commands to a sage worksheet) is easy; just upload it through the
notebook interface.

-- Tianwei

David Sanders

unread,
Jul 8, 2010, 2:26:01 PM7/8/10
to sage-support


On Jul 8, 5:45 am, TianWei <ltwis...@gmail.com> wrote:
> > I built a worksheet with several cells in the notebook, and then
> > wanted to try to run it on a remote computer using the command line
> > interface.  But I could not find a convincing way to export the
> > worksheet to a simple text file that I could import directly to sage,
> > i.e. a "something.sage" file.
> > In the end, I had to copy and paste each cell separately into a text
> > file on the remote machine, which I then imported with "load
> > something.sage"  But this is clearly not a reasonable solution for a
> > long worksheet.
> > I tried the "Text" option in the worksheet, but that produced output
> > which I could not just copy straight into the sage command line, or
> > import with "load".
>
> If Sage has an easy, automatic way of doing this, I'm not aware of it.
> However, there's a couple ways to accomplish what you want to do
> without doing a bunch of repetitive actions (for example, manually
> copying out each the text cell). Unfortunately, both of my suggestions
> require a bit of work:
>
> (1) Open the worksheet through the notebook interface, then click on
> the "Text" tab. Copy the text there, then write a little script to
> strip out every line that doesn't begin with "sage: ". The lines that
> have "sage: " prepended are lines in the input cells (assuming you
> didn't type "sage: " somewhere in the worksheet itself). Strip out the
> "sage: " text.

OK, this is the idea that I came up with.
It is also necessary to strip out the "..." that come at the start of
indented lines, the <html> lines corresponding to the pretty-printed
output, etc.

I guess I just assumed that this must already have been done and be
easily accessible, since it seems like a reasonably obvious thing to
want to do -- use the nice notebook interface to create a document,
and then convert it to plain Python and/or Sage to use for whatever.

>
> (2) You could tinker around with Notebook, Worksheet, and Cell objects
> in Sage (using a script, command-line, or notebook interface). In
> summary, you could create a Notebook object; get the desired Worksheet
> object using one of the methods of the Notebook object; get a list of
> Cell objects from the Worksheet; then pull out the input text from
> each Cell object. The documentation for these objects are:
>
> http://www.sagemath.org/doc/reference/sagenb/notebook/notebook.htmlhttp://www.sagemath.org/doc/reference/sagenb/notebook/worksheet.htmlhttp://www.sagemath.org/doc/reference/sagenb/notebook/cell.html
>
> Again, I'm (obviously) not aware of a cleaner way to just pull out the
> input text from a worksheet, and these two suggestions are just the
> work-arounds I could think of.

Thanks to all for the suggestions.

David.

Perez Verona Isabel Cristina

unread,
Jul 23, 2019, 10:32:02 AM7/23/19
to sage-support
Hi David,

I'm facing same problem.
Did you found an elegant way to solve this problem?

Simon King

unread,
Jul 23, 2019, 10:40:59 AM7/23/19
to sage-s...@googlegroups.com
Hi,

The post you are replying to is about 9 years old. Meanwhile, it is (I
think) recommended to not use the legacy Sage notebook (sagenb), but use
jupyter.

So, do you really have the problem of converting a Sage notebook, or a
jupyter worksheet?

Best regards,
Simon
Reply all
Reply to author
Forward
0 new messages