How to properly set up this Sage Cell Server? - python

I have a question regarding the Sage Cell Server. It is a relatively general question, I don't have any code that I need fixed.
I am trying to set up a server but I am finding the world of servers confusing. I've worked plenty with Java and JavaScript, HTML and CSS on frontend stuff, but for the backend Sage server I decided to develop I figured Python was a good choice. Some basic Lynda tutorials and a book later, I have a pretty good idea how python works... But am still lost. My question is basically this:
-I am trying to design a very simple server running an instance of Sage. I want to be able to SEND Sage code to it (NOT PYTHON) and have it evaluated and the result sent back, as if I were running sage on my own computer. Example: Sending "integrate(x,x)" to the server would return 1/2*x^2
-What is the best way to go about doing this? Should I download the Sage Cell Server?
-Will I need to even write Python code for this? How long would this task take in hours?
-Would it be easier for me to design the server to receive and execute Python code instead of Sage code? How would I go about doing this?
If you are wondering why I want to do this, I want to design a website where people can type in their math problems and have them evaluated by sage. I know there are simpler ways to do this but I've wanted to do server-side work and get people talking with a server for a while now and this seems like a good introduction to it.
Any and all advice is appreciated, resources, help, etc.

There are certainly a number of people who have successfully set up their own Sage cell server, and as such things go I hear it is definitely doable. The previous "simple server" API is pretty much defunct and the Sage cell is a pretty reasonable way to do this. I don't see why you would need to have reinvent the wheel here, and asking on sage-support#googlegroups.com is a logical next step for you.

Related

How can I call a python's local function from an HTML doc?

I have written a python file with several functions. Now I'm writting a web page where I want to call a function of the file that I mentioned before, by pressing a button but I don't know how.
Can someone help me, or telling me where can I look for some example where python and html are connected.
I'm going to talk only about a front end solution , well known solution for back end only exist (Django) and the others will certainly talk about it.
Beforehand, you should know that using Python as a front end web language is very strange.
The "King" of these languages is JavaScript, and if you want to go further with web development, especially with front end frameworks (AngularJS, react), you should probably learn it.
That being said, there is a way to achieve what you want.
Either with this Python framework, or with a transpiler.
First, you need to set up a server to handle HTTP requests.
Then, something that can interpret python codes is needed. mod_wsgi is a good choice for python. But for a simpler way, you can also use CGI (take a look here).
Edit:
A more complete/structured solution is using a python web framework. For instance, Django or Flask. But be prepared to spend more time on them.

newbie: writing backend code for website

I am usually working in fields of machine learning and hence my background is mostly in stats/ML and no formal web background.
Usually for my project, I work on python which is connected to my local mysql db... to fetch data adn everything.
Now, my work is mostly complete.. everything is console based..
(like traditional programs).
How do I integrate it on the front end. I understand that this is more like a server side scripting.
So, lets take an example of google.
In the front end.. someone enters a search query.. and in the backend lets say there is a program in C++ which executes that query.
How did this interaction takes place.. if front end is written in lets say php..
I assume shell execution of program is a bad bad way to run programs.. ??
Any suggestion will be greatly appreciated.
Thanks
As suggested by Ignacio, you will first need to design API for your project. This is basically clearly laying out what (and how) queries will be supported for your project.
You don't need shell execution and neither need to learn PHP. Since your project is in Python, you can use Python Web-frameworks like Django, Web2Py.
The first thing to do is to develop an API for your library. From there you can develop multiple frontends that use the same API in order to present it multiple ways.
Decouple your user interface logic from your business logic, then reuse the business logic libraries in an application that accepts input over HTTP instead of the console. Django is a popular web framework that will take care of a lot of the front end concerns for you, or you can use something like CGI if you prefer to be a bit closer to the bare metal. The Python Wiki has a section on web programming that you might find useful.

python script output to webpage

I wrote a python script that reads in text from a file and writes a text file of definitions. I want to somehow integrate my program with a webpage for the whole world to see.
I want to be able to retrieve input text from one text box, have the python script process it, then display the output in the other text box.
I have done quite a bit of research thus far but I am still unsure of the best way to go about doing this. I tried using google's app engine but encountered too many problems, for example the app engine runtime environment uses python 2.5.2, I wrote my program using 3.1.2. Other than that I just felt that I was beginning to waste my time trying to port my program over.
I'm starting to think that javascript is the way to go or maybe pyjamas. I was also wondering if it would be possible to just have the python program constantly running on the server and to perform a system call.
I posses very little knowledge when it comes to web development. I appreciate any advice.
You could use the cgi module and create a CGI script, if your server supports it.
It's a much bigger question, involving:
Where are you going to host the site,
How slow is the script (can it execute in a few seconds or not),
Does it need access data from files or a database,
How complex is it,
etc.
I would suggest you read about Django:
http://docs.djangoproject.com/en/1.2/
That is probably the easiest way to set up a simple web site, but is also very powerful if you want to do something more in the future (related to this project or not).
However, since your script is Python 3 only, you don't have too many options, see this question:
https://stackoverflow.com/questions/373945/what-web-development-frameworks-support-python-3
I suppose, if not too hard, it is worth thinking about converting it to Python 2.7.
If that is an option, then you might very well go down to Python 2.5 and use Google App Engine. It gives you many things for free and you really don't need to worry about many things that you would if you were to set up your server. It includes a modified (better to say, shrunk down) version of Django 1.1. When you say you are wasting your time porting from 3.x to 2.5, I guess you were not counting the time that you will waste setting all other things up.

Would it be a good idea or bad idea to connect a VB.NET frontend with a Python backend using sockets?

I have some really nice Python code to do what I need to do. I don't particularly like any of the Python GUI choices though. wxPython is nice, but for what I need, the speed on resizing, refreshing and dynamically adding controls just isn't there. I would like to create the GUI in VB.NET. I imagine I could use IronPython to link the two, but that creates a dependency on a rather large third-party product. I was perusing the MSDN documentation on Windows IPC and got the idea to use sockets. I copied the Python echo server code from the Python documentation and in under 5 minutes was able to create a client in VB.NET without even reading the System.Net.Sockets documentation, so this certainly doesn't seem too hard.
The question I have is... is this a terrible idea? If so, what should I be doing instead?
If this is a good idea, how do I go about it?
It's not a terrible idea. In fact, if you write the Python code to have a RESTful interface, and then access that from VB.NET, it is a downright good idea. Later on you could reuse that Python server from any other application written in Python or VB.NET or something else. Because REST is standard and easy to test, people can even do GETs from a browser and maybe that will be useful in itself.
Here is a Yahoo page that gives you code examples to do REST GET, POST and so on, in VB.NET.
If you think REST has too much overhead and need something more lightweight, please don't try to invent your own protocol. Consider something like Google's Protocol Buffers which can also be used from VB.NET.
I think this is an excellent idea. I'll second Michael Dillon's recommendation for a REST API, and I'll further recommend that you use Django to implement your REST server.
I wrote a REST web service using Django, and Django made it really easy and fun. Django made it really simple to set up the URLs the way I wanted them, to run whatever code a URL called for, and to interact with the database as needed. My web service was rock solid reliable, and I was able to test it for debugging simply using a web browser.
If you already have your code working in Python and just want to slap on a glue interface, and if REST doesn't seem like what you want, you could look at the Twisted networking framework. Here is a nice article on how to do networking in Python with both the standard Python modules and with Twisted.

python html integration

this is a complete n00b question and i understand i may get voted down for asking this but i am totally confused over python's html integration.
as i understand one way to integrate python with html code is by using mod_python.
now, is there any other way or method that is more effective for using python with html?
please advise me on this as i am new to learning python and could use some help.
some pointers to code samples would be highly appreciated.
thanks a lot.
EDIT: also what i would like to know is, how does PyHP and mod_python compare with regards to each other. i mean how are they different? and Django? what is Django all about?
I would suggest you to start with web.py
You can read a tutorial on how to use Python in the web.
http://docs.python.org/howto/webservers.html
In few words, mod_python keeps python interpreter in memory ready to execute python scripts, which is faster than launching it every time. It doesn't let you integrate python in html like PHP. For this you need to use a special application, like PyHP (http://www.pyhp.org) or another (there are several of them). Read Python tutorial and documentation pages, there's plenty of info and links to many template and html-embedding engines.
Such engines as PyHP require some overhead to run. Without them, your python application must output HTTP response headers and the page as strings. Mod_wsgi and fastcgi facilitate this process. The page I linked in the beginning gives a good overview on that.
Also you may try Tornado, a python web server, if you don't need to stick to Apache.
The standard way for Python web apps to talk to a webserver is WSGI. Also check out WebOb.
http://www.wsgi.org/wsgi/
http://pythonpaste.org/webob/
But for a complete noob I'd start with a complete web-framework (in which case you typically can ignore the links above). Django or Grok are both full-stack framworks that are easy to use and learn. Django is more popular, but Grok is built on 13 years of Web application publishing experience, and is seriously cool. The difference is a matter of taste.
http://django.org/
http://grok.zope.org/
If you want something more minimalistic, the worlds your oyster, there are an infinite amount of web frameworks for Python, from BFG to Turbogears.

Categories