Sagemath with XMLRPC

11 views
Skip to first unread message

NoSyu

unread,
Jul 22, 2009, 12:31:41 PM7/22/09
to sage-devel
Hello.
I use blog and Sagemath. Sagemath Notebook is great, but I want to
write post in my blog with Sagemath code and result(output).
So I add the XMLRPC server code in server/notebook/twisted.py and make
the APIKEY each user a substitute for password. Because XMLRPC doesn’t
have own secure policy.


############################
# XMLRPC
# 20090709, Bak JinYeong(don...@skku.edu)
# reference page
# http://twistedmatrix.com/trac/wiki/Documentation
# http://twistedmatrix.com/projects/web/documentation/howto/xmlrpc.html
# http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.html
# http://twistedmatrix.com/documents/8.1.0/api/twisted.web2.xmlrpc.XMLRPC.html
# http://twistedmatrix.com/documents/8.1.0/api/twisted.internet.defer.html#maybeDeferred
############################
from twisted.web2 import xmlrpc # Add to use xmlrpc

class XMLRPC_Sage(xmlrpc.XMLRPC):
def __init__(self, request):
self.request = request

# check the username and apikey
def login(self, username, apikey):
return apikey == notebook.user(username).apikey()

def xmlrpc_make_worksheet_txt(self, request, args):
print "XMLRPC Request, function name : make_worksheet_txt
Start"
try:
# set vaule from xml
worksheet_txt = args['txt']
username = args['username']
apikey = args['apikey']
except KeyError:
# If xml is not corrected, send error message.
raise KeyError
else:
import worksheet
# get the worksheet name to find the exist worksheet in
server
worksheet_name, _ = worksheet.extract_name(worksheet_txt)
# set default value
update = False
# if it is in xml request, replace it.
if 'update' in args:
update = args['update']

# check the username and apikey
if self.login(username, apikey):
try:
# search the same name worksheet
worksheet = notebook.get_worksheet_with_name
(worksheet_name)
except KeyError:
# no one in notebook, so make new worksheet.
worksheet =
notebook.new_worksheet_with_title_from_text(worksheet_txt, username)
update = True

if update:
# update the worksheet
worksheet.edit_save(worksheet_txt)

# delete published worksheet because if it is
published it do not evaluate cell.
if worksheet.has_published_version():
notebook.delete_worksheet
(worksheet.published_version().filename())

# get the cells
cells = worksheet.cell_list()
# evaluate the all cells
for cell in cells:
cell.evaluate(username = username)
# wait until cell is done. There is not error
handle code.
cell_id = cell.id()
status, _ = worksheet.check_comp(wait=9999)
while 'e' != status:
status, _ = worksheet.check_comp
(wait=9999)

# quit the worksheet
worksheet.quit()

# worksheet publish
P_worksheet = notebook.publish_worksheet
(worksheet, username)

# return the worksheet with output
# change imgsrc
import re
addr = 'http%s://' % ('' if not notebook.secure else
's')
addr += notebook.address if not '' == notebook.address
else 'SAGEADDR'
addr += '%s' % ('' if 80 == notebook.port else (':' +
str(notebook.port)))
addr += '/home/' + P_worksheet.filename() + "/cells/"
ReC = re.compile("id=(\d+?)(.*?)'cell://(.+?)'",
re.DOTALL)
txt_cells = P_worksheet.edit_text().split('{{{')
txt_cells = map(lambda txt_cell:ReC.sub(r"id=\1\2'" +
addr + r"\1/\3'", txt_cell), txt_cells)
print "XMLRPC Request, function name :
make_worksheet_txt Finish"
return '{{{'.join(txt_cells)
else:
# I can't handle this error, so just raise (Exception)
hand.^^
raise ValueError



class Toplevel(resource.PostableResource):
child_login = RedirectLogin()
child_simple = sage.server.simple.twist.SimpleServer()
child_xmlrpc = XMLRPC_Sage # Add to use xmlrpc - 20090709, Bak
JinYeong(don...@skku.edu)


And I make Textcube(it’s blog tool made in Korea) and WordPress
sagemath XMLRPC client plugin. It works well.
It works like latex plugin.

[Sagemath]3*4*5*6[/Sagemath]
=>
Sage: 3*4*5*6
360

http://nosyu.pe.kr/2048
http://nosyu.mireene.kr/wp/wordpress/


Now I have questions. Please reply it.

1. Can this XMLRPC code add into the public Sagemath source?
It adds several codes and function in list_top.html and user.py. So I
think it’s hard.OTL

2. Is it worth?
I make this code for my desire. So I ask to other that is it worth to
public?

3. Is it legal?
I don’t know about GPL exactly. I just know I can see the source,
modify it and open to the public. But is it illegal that I don’t open
this source?

4. I make this on sage 4.0.2. But latest sage version is 4.1 and
Notebook is rapidly changed. Then it will be changed notebook server
module from twisted to something? I make this to use twisted web2
XMLRPC server. However if it is changed, I can’t use it. So I am
worried about it.

------------------------------------------------------------------------

누구나가 다, 자기 옆에서 눈물을 흘리며 신음하는 불행한 사람들에 비해 자기가 훨씬 더 불행하다고 생각하지요. 이게 바로 우리
가련한 인간들의 오만 중 하나입니다.
- 몬테크리스토 백작
it is the infirmity of our nature always to believe ourselves much
more unhappy than those who groan by our sides!
- The Count of Monte Cristo
c'est un des orgueils de notre pauvre humanité, que chaque homme se
croie plus malheureux qu'un autre malheureux qui pleure et qui gémit à
côté de lui
- Le Comte de Monte-Cristo

박진영 - Bak JinYeong
학부재학생 - Undergraduate
컴퓨터공학전공 - Department of Computer Engineering
정보통신공학부 - School of Information & Communication Engineering
성균관대학교 - SungKyunKwan University
블로그 - http://nosyu.pe.kr
이메일 - don...@skku.edu

Dan Drake

unread,
Jul 22, 2009, 4:29:09 PM7/22/09
to sage-...@googlegroups.com
Hello JinYeong,

I don't understand much about XMLRPC, but I'll try to answer some of
your questions:

On Wed, 22 Jul 2009 at 09:31AM -0700, NoSyu wrote:
> 1. Can this XMLRPC code add into the public Sagemath source?
> It adds several codes and function in list_top.html and user.py. So I
> think it’s hard.OTL
>
> 2. Is it worth?
> I make this code for my desire. So I ask to other that is it worth to
> public?

I visited your example blog (http://nosyu.mireene.kr/wp/wordpress/) and
I see that it automatically includes the plots and calculations. This
seems like a great contribution to Sage and I think other people would
find it useful.

> 3. Is it legal?
> I don’t know about GPL exactly. I just know I can see the source,
> modify it and open to the public. But is it illegal that I don’t open
> this source?

You wrote the code, so it is yours and you can do whatever you like with
it. However, it is very unlikely that your contribution will be put into
the main Sage library unless you make the code GPL'ed. I hope you let us
include your code and distribute it under the GPL.

Dan

--
--- Dan Drake <dr...@kaist.edu>
----- KAIST Department of Mathematical Sciences
------- http://mathsci.kaist.ac.kr/~drake

signature.asc

NoSyu

unread,
Jul 22, 2009, 11:30:08 PM7/22/09
to sage-devel
Dear Prof Dan.

Thanks to reply my post.

I also don't know about XMLRPC exactly, but I can show the process.

XMLRPC is Remote Procedure Control to use XML. So I have sagemath
server and another blog server. I can use sage code in blog server
using XMLRPC.

So I write the post on my blog like this.


Simple Test!

[sagemath]A = matrix(4, 2, [1, 0, 1, 1, 1, 2, 1, 3])
print(A)[/sagemath]

[sagemath]b = matrix(4, 1, [1, 3, 4, 4])
print(b)[/sagemath]

[sagemath]A.rank()[/sagemath]

[sagemath]Ad = ((A.transpose() * A).inverse()) * A.transpose()
print(Ad)[/sagemath]

[sagemath]lssx = Ad * b
print(lssx)[/sagemath]

[sagemath]x = var('x')
P = point(((0, 1), (1, 3), (2, 4), (3, 4)))
P += plot(lssx[0, 0] + lssx[1, 0] * x, -1, 5, rgbcolor='red')
P.show(xmin=-1, xmax=5, ymin=0, ymax=5)[/sagemath]


Then wordpress plugin make worksheet based on txt like this.


http://nosyu.mireene.kr/wp/wordpress/26
system:sage
{{{
A = matrix(4, 2, [1, 0, 1, 1, 1, 2, 1, 3])
print(A)
}}}
{{{
b = matrix(4, 1, [1, 3, 4, 4])
print(b)
}}}
{{{
A.rank()
}}}
{{{
Ad = ((A.transpose() * A).inverse()) * A.transpose()
print(Ad)
}}}
{{{
lssx = Ad * b
print(lssx)
}}}
{{{
x = var('x')
P = point(((0, 1), (1, 3), (2, 4), (3, 4)))
P += plot(lssx[0, 0] + lssx[1, 0] * x, -1, 5, rgbcolor='red')
P.show(xmin=-1, xmax=5, ymin=0, ymax=5)
}}}


Then send this to sagemath server to use XMLRPC.

Sagemath server got this, and make the worksheet, evaluate each cell,
publish it and return the worksheet with result.


http://nosyu.mireene.kr/wp/wordpress/29
system:sage

{{{id=0|
A = matrix(4, 2, [1, 0, 1, 1, 1, 2, 1, 3])
print(A)
///
[1 0]
[1 1]
[1 2]
[1 3]
}}}

{{{id=1|
b = matrix(4, 1, [1, 3, 4, 4])
print(b)
///
[1]
[3]
[4]
[4]
}}}

{{{id=2|
A.rank()
///
2
}}}

{{{id=3|
Ad = ((A.transpose() * A).inverse()) * A.transpose()
print(Ad)
///
[ 7/10 2/5 1/10 -1/5]
[-3/10 -1/10 1/10 3/10]
}}}

{{{id=4|
lssx = Ad * b
print(lssx)
///
[3/2]
[ 1]
}}}

{{{id=5|
x = var('x')
P = point(((0, 1), (1, 3), (2, 4), (3, 4)))
P += plot(lssx[0, 0] + lssx[1, 0] * x, -1, 5, rgbcolor='red')
P.show(xmin=-1, xmax=5, ymin=0, ymax=5)
///

}}}


Then blog server receive this worksheet, and chage the good looking
format like this.


Simple Test!
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : A =
matrix(4, 2, [1, 0, 1, 1, 1, 2, 1, 3])
print(A)</div>
[1 0]
[1 1]
[1 2]
[1 3]</blockquote>
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : b =
matrix(4, 1, [1, 3, 4, 4])
print(b)</div>
[1]
[3]
[4]
[4]</blockquote>
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : A.rank()
</div>
2</blockquote>
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : Ad =
((A.transpose() * A).inverse()) * A.transpose()
print(Ad)</div>
[ 7/10 2/5 1/10 -1/5]
[-3/10 -1/10 1/10 3/10]</blockquote>
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : lssx =
Ad * b
print(lssx)</div>
[3/2]
[ 1]</blockquote>
<blockquote>
<div style="padding: 10px; background-color: #c9edff;">sage : x = var
('x')
P = point(((0, 1), (1, 3), (2, 4), (3, 4)))
P += plot(lssx[0, 0] + lssx[1, 0] * x, -1, 5, rgbcolor='red')
P.show(xmin=-1, xmax=5, ymin=0, ymax=5)</div>
<span style="color: black;"><img src="http://NoSyu.homeip.net:8000/
home/pub/76/cells/5/sage0.png" alt="" /></span></blockquote>


http://nosyu.mireene.kr/wp/wordpress/?p=29


So I make XMLRPC class in sagemath server and wordpress XMLRPC client
plugin.



I think this code's license is GPL.

But how can I show this source?

upload the file? attach the code in this message?


Thanks again to reply my post.

Have a good day!^^


------------------------------------------------------------------------

누구나가 다, 자기 옆에서 눈물을 흘리며 신음하는 불행한 사람들에 비해 자기가 훨씬 더 불행하다고 생각하지요. 이게 바로 우리
가련한 인간들의 오만 중 하나입니다.
- 몬테크리스토 백작
it is the infirmity of our nature always to believe ourselves much
more unhappy than those who groan by our sides!
- The Count of Monte Cristo
c'est un des orgueils de notre pauvre humanité, que chaque homme se
croie plus malheureux qu'un autre malheureux qui pleure et qui gémit à
côté de lui
- Le Comte de Monte-Cristo

박진영 - Bak JinYeong
학부재학생 - Undergraduate
컴퓨터공학전공 - Department of Computer Engineering
정보통신공학부 - School of Information & Communication Engineering
성균관대학교 - SungKyunKwan University
블로그 - http://nosyu.pe.kr
이메일 - don...@skku.edu


>  signature.asc
> < 1K보기다운로드

Minh Nguyen

unread,
Jul 23, 2009, 2:29:59 AM7/23/09
to sage-...@googlegroups.com
On Thu, Jul 23, 2009 at 2:31 AM, NoSyu<don...@gmail.com> wrote:
>
> Hello.
> I use blog and Sagemath. Sagemath Notebook is great, but I want to
> write post in my blog with Sagemath code and result(output).
> So I add the XMLRPC server code in server/notebook/twisted.py and make
> the APIKEY each user a substitute for password. Because XMLRPC doesn’t
> have own secure policy.

The following sage-devel thread talks about using XML-RPC in the
notebook rewrite effort:

http://groups.google.com/group/sage-devel/browse_thread/thread/65ca1e0489a0a980

--
Regards
Minh Van Nguyen

Reply all
Reply to author
Forward
0 new messages