0

this question have been asked numerous times, I know and I'm sorry if by ignorance I didn't get the answers.

I have a hosting plan which doesn't allow me to install django, which was really nice to call an rest api easily with the routing settings.

What I want is to be able to call a python function from javascript code doing a get/post (I'm using AngularJs, but it would be the same making an ajax get/post.)

Let's say I have a js controller 'Employee' and a view 'CreateEmployee'.

From my javascript view, I can call my CreateEmployee() on the js controller, now my question is, how can I call a specific function (let's say) def CreateEmployee(params...) on my .py file?

All I found is making a get/post on my .py file, but didn't find how to invoke a specific function.

I probably don't get the python and client/server communication paradigm, I've been coding on asp.net WebForms for a long time, and since I can't use frameworks like Django I'm stuck.

Thanks

0

2 Answers 2

2

It doesn't have much to do with Python, really. Your javascript code is executed on the client's brower, and all it can do is issuing HTTP requests (synchronous or asynchronous). At this point which webserver / technology / language is used to handle the HTTP request is totally irrelevant. So, from the client javascript code POV, you are not "calling a Python function", you are sending an HTTP request and handling the HTTP response.

If your web host doesn't let you run django (or any wsgi-compliant script) then you'll probably have to either use plain CGI (warning: very primitive techno) or migrate to PHP (no comment). Or find another hosting that doesn't live in the past ;)

1
  • That would have been my last option, I don't want to switch to php, I wanted to learn something new on the open source sphere, and choosed python and django, which was surprising good. And then I realized my hosting plan didn't had django neither flask... Commented Jul 28, 2014 at 10:45
0

You must have a way to communicate over HTTP with your server side Python code. This could help.

When you do, you communicate over HTTP. You will not invoke Python functions directly.

8
  • Thanks, but I still have the same problem. The hosting that they gave me accepts python, but doesn't include django neither flask... Commented Jul 28, 2014 at 10:42
  • I see. I would find another hosting because this is a major limitation to Python usage. Or you could just use another language to implement an HTTP API. That could be for example node.js (JavaScript) since you already know JS. Commented Jul 28, 2014 at 10:45
  • @ElBarto: how does your hosting "accept python" ? CGI or wsgi ? Commented Jul 28, 2014 at 10:57
  • @bruno: I don't know.. :/ Actually they gave me access to a virtual server on 1&1, I may be able to install django via ssh, but I'm really new on this subject and I'm afraid of typing something wrong, it is a prod server... Commented Jul 28, 2014 at 13:19
  • If it's a dedicated server, you should be able to install whatever you want - or, at least, to ask the server's sysadmin to to so. You should not rely on a system wide install of Django anyway, better to have you own exact versions of all your dependencies in a virtualenv. Commented Jul 28, 2014 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.