Accessing Files

39 views
Skip to first unread message

Ingo Dahn

unread,
Aug 28, 2017, 8:30:08 AM8/28/17
to sage-cell
Is there a way to access (possibly through user interaction) files on a local computer, in order to use - for example - R's read.table()?

Andrey Novoseltsev

unread,
Aug 29, 2017, 12:52:22 AM8/29/17
to sage-cell
On Monday, 28 August 2017 06:30:08 UTC-6, Ingo Dahn wrote:
Is there a way to access (possibly through user interaction) files on a local computer, in order to use - for example - R's read.table()?

I think something like this was achieved by https://github.com/sagemath/sagecell/blob/master/js/cell.js#L247 (and other places, probably). I've never looked at it which probably means that it has to be modified to work at all with the current version. Not sure what were the issues and why it was turned off.

It is possible to load files from other servers, for local computer a possible solution may be to create a multiline textbox used for an interact control and copy-paste file there. I've done something like this a few years ago but don't have the code.

Ingo Dahn

unread,
Aug 29, 2017, 2:27:07 AM8/29/17
to Andrey Novoseltsev, sage-cell
That's interesting - how can I load a file from another server? Can I make an API/Curl call from within a SageCell?

--
You received this message because you are subscribed to a topic in the Google Groups "sage-cell" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-cell/__tQ_iG9FZQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-cell+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sage-cell/5e20ce80-e015-4900-8d1d-421cfa2dd227%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrey Novoseltsev

unread,
Aug 29, 2017, 11:16:48 AM8/29/17
to sage-cell, novo...@gmail.com
You can use urllib to fetch files and store them locally (in the current directory which is different for every set of linked cells), then open in R unless it has similar commands on its own. We don't have any dedicated custom API for fetching files.

Ingo Dahn

unread,
Aug 29, 2017, 11:38:22 AM8/29/17
to Andrey Novoseltsev, sage-cell
I confess that I am not familiar with Python. When I naively import urllib in a SageCell and call urllib.request.urlopen() I get the error "AttributeError: 'module' object has no attribute 'request'").
A working example would be welcome.

Andrey Novoseltsev <novo...@gmail.com> schrieb am Di., 29. Aug. 2017 um 17:16 Uhr:
You can use urllib to fetch files and store them locally (in the current directory which is different for every set of linked cells), then open in R unless it has similar commands on its own. We don't have any dedicated custom API for fetching files.

--
You received this message because you are subscribed to a topic in the Google Groups "sage-cell" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sage-cell/__tQ_iG9FZQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sage-cell+...@googlegroups.com.

Andrey Novoseltsev

unread,
Aug 30, 2017, 12:58:07 AM8/30/17
to sage-cell, novo...@gmail.com
For example I can do this

import urllib
urllib.urlretrieve('https://raw.githubusercontent.com/sagemath/sagecell/master/LICENSE.txt', 'local.txt')

and the file becomes available and gets a link under a cell. Via

import urllib2
print urllib2.urlopen('https://raw.githubusercontent.com/sagemath/sagecell/master/LICENSE.txt', None).read()

I can see the content. No claims that these are the best ways to handle the situation ;-)

Tevian Dray

unread,
Apr 16, 2018, 11:00:19 PM4/16/18
to sage-cell
Thank you for this helpful tip, which appears to allow one to load code from a URL.  I successfully tried:

import urllib
url="URL"
exec(eval(urllib.urlopen(url).read()))

with URL pointing to a file on my website containing (just for proof-of-concept):

"""
def f(x):
    return x**2
a=3
"""

There may be a better way, but this appears to work -- so long as the file contains python code; sage extensions may not work.
Reply all
Reply to author
Forward
0 new messages