Can you drop a Python script into a Django page?

25 views
Skip to first unread message

jc

unread,
Nov 12, 2011, 5:32:02 AM11/12/11
to django...@googlegroups.com
Hi,

Is it possible to drop a Python script (around 50 lines) into one of your django pages? Would this be embedding into the template code? I am experimenting with doing this but I haven't had any luck in finding out how.

An example of what I am trying to do is to place a script like this http://www.goldb.org/ystockquote.html and see if I can't drop it into a page like this http://demo.satchmoproject.com/category/book/fiction/ and have it print out the info on the page.

First thing that comes to mind is once I figure out how to embed the script, how will I execute it? 

thanks,
jimmy

Joey Espinosa

unread,
Nov 12, 2011, 6:38:25 AM11/12/11
to django...@googlegroups.com

Jimmy,

Does it NEED to be embedded into the page? I ask because something like this would be a lot easier if you executed server-side, and simply returned the output to the front-end using AJAX.

Would that serve your purpose here, or am I misunderstanding the original question?

--
Joey "JoeLinux" Espinosa
Software Developer
http://about.me/joelinux

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/heUngZyYuE8J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

jc

unread,
Nov 12, 2011, 6:43:44 AM11/12/11
to django...@googlegroups.com
Oh, that would totally be fine. I have no idea how to do that, do you know of any docs that might show me how to execute this code on the server-side and print out via AJAX?

thanks!
jimmy

Joey Espinosa

unread,
Nov 12, 2011, 8:50:22 AM11/12/11
to django...@googlegroups.com
Jimmy,

Not sure what JavaScript library you use (or if you're familiar with JavaScript at all), but this is a very rudimentary example:

First, create a View in Django that you can call, and capture the results:

import os
from django.http import HttpResponse
...
def ajax(req):
    if req.is_ajax():
        stdout = os.popen("script_you_want_to_run.py")
        output = stdout.read()
        return HttpResponse(output)

Make sure you can call it by adding it in your urls.py (be sure that it's defined above any other line that may redirect you somewhere else):

urlpatterns = patterns('',
    url(r'^ajax', 'your_project.your_app.views.ajax'),
    ... 

Then call it from whatever template you're wanting to use (I'm going to use Dojo for this example, just because it's my favorite JavaScript library):

var resultDisplay = dojo.byId("results");  // whatever element you want to display your results
dojo.xhrGet({
    url: '/ajax',  // this accesses the view you created earlier
    load: function(data) {
        resultDisplay.innerHTML = data;  // 'data' should be the output that you returned from the view
    }
});

Again, this is very basic, but this should get you started. Use some test scenarios to try it out just so you get the idea (such as dropping in a Python script that does nothing but output "Hello").

I hope this helped.
    
--
Joey "JoeLinux" Espinosa




jimmy

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/KnKT8mJOggUJ.

jc

unread,
Nov 12, 2011, 9:30:52 AM11/12/11
to django...@googlegroups.com
Oh, this does help...tremendously. Thanks for taking the time out and explaining. I think that's enough for me to start learning via the docs/experimenting and hopefully get something implemented.

If I can't implement this, do you know anyone that could do this for a fee?

thanks,
jimmy

Joey Espinosa

unread,
Nov 12, 2011, 9:52:37 AM11/12/11
to django...@googlegroups.com

Jimmy,

I don't know your specific situation, but this is probably not worth a fee. You already know Django and Python, all you need to do is extend that knowledge with some AJAX.

If you run into problems, there's always this group ;)

--
Joey "JoeLinux" Espinosa
Software Developer
http://about.me/joelinux

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/LiX0uN29AQYJ.

Steve McConville

unread,
Nov 12, 2011, 1:38:01 PM11/12/11
to django...@googlegroups.com
> Is it possible to drop a Python script (around 50 lines) into one of your
> django pages? Would this be embedding into the template code?

You could wrap it in a custom template tag:

https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/

--
steve

Reply all
Reply to author
Forward
0 new messages