1

I'm using splunk and its framework that works only in javascript and Django. I would like open an external file and edit it, but with javascript is impossible and I'm not sure that with Django. If anyone know, please tell me how do this.

So, I thought that I can call an external script from javascript or Django, in python for example, that opens my file to edit, and send the results at my javascript page.

The file to edit is stored in the a different subfolder than my javascript page, but both of them are in the same splunk_app folder:

Such as

home/splunk/apps/name/django_template/file_java.js
home/splunk/apps/name/django_template/script_that_edit.py
home/splunk/apps/name/lookup/file_to_edit.csv 

Thank you

2
  • 1
    you can open a file in Python (i.e. in a Django view) and send the result to your javascript app docs.python.org/2/library/functions.html#open Commented Feb 18, 2015 at 11:45
  • thank you for your reply but I would like call python from javascript, not javascript from python Commented Feb 18, 2015 at 13:36

2 Answers 2

1

Thank you for your help, I'm trying to use the splunk binding, so I have take the three files: urls, views and mypage.html So I have add the url:

url(r'^mypage/$', 'mynewapp.views.myview', name='mypage'), 

the render function:

@render_to('mynewapp:mypage.html')
@login_required
def myview(request):
with open(csvfile, "r+") as lines:      
   for line in lines:
      file_data += line
service = request.service
return file_data

and the mypage.html django code:

{% for data in file_data %}
{{ data }}
{% endfor %}

But there is something of wrong, but I don't understand what.

Instead of file_Data, I'm going to use a dictionary, but how can I Execute myscript python file using subprocess from mypage.html?

Thank you so much for your help.

Sign up to request clarification or add additional context in comments.

1 Comment

I would like ask you something else, how I can run the subprocess? I have never used them before.
0

Splunk is built on the django web framework. What you are trying to do is to execute code on the server side. To do that your will need to write your own view that will either

  1. Execute your script python file using subprocess
  2. Embedding your code with the view

In vanilla django, assuming you have route the address to the correct view in your urls.py ,your views.py function should look like this : https://docs.djangoproject.com/en/1.7/topics/http/views/

from django.shortcuts import render ## function to return a response and render a html tempalte

def myView(request):
    results = {} ## a dictionary to store your results/can be an array as well
    ## your function here and store your data in results
    with open('file.csv') as f:
        ...
        ... 

    return render(request, 'template.html', results ) 
    ## you will be returning a javascript object call results

and in your template you can either access them by the django template syntax

{% for data in results %}
<p>{{ data }}</p>

or you can store it in a variable

var data = {{ results|safe }}

Alternatively, splunk provide some bindings for doing your custom view. You can check out http://dev.splunk.com/view/SP-CAAAEMP

In principle it is the same. run your code within the function and return it as a dictionary .

@render_to('your_app_name:pythondemo.html')
@login_required
def pythondemo_view(request):
    file_data = '' 
    with open('file.csv') as f:
       for line in f : 
           file_data += line

    return file_data ## 

this time you dont need to return a render becuase the splunk decorator will do it for you @render_to

To do a subprocess, check out http://pymotw.com/2/subprocess/

You might run in problems with file read/write/execution permissions problem, so I will strongly suggest that you place your code in the the request.

Either wise, since it is a python script, you can just wrap up your code in a function, and import it. For instance,

in yourscripts.py

#!/usr/bin/env python
def myFunction():
    return 1 + 1 

and in your view.py do:

from youscripts import myFunction

Let me know if you need any more help. You may need to ajdnust the code accordingly. If you want to call an external python script, do a subprocess command

4 Comments

Thank you for your help, I'm trying to use the splunk binding, so I have take the three files: urls, views and mypage.html and I have add the url: url(r'^mypage/$', 'mynewapp.views.myview', name='mypage'), the render function: @render_to('mynewapp:mypage.html') @login_required def myview(request): with open(csvfile, "r+") as lines: for line in lines: file_data += line service = request.service return file_data and {% for data in file_data %} <p>{{ data }}</p> {% endfor %} in the mypage.html, but there is soething of wrong I think
Look at the answer below : )
Hi Federica, you usually add any edition to your questions and not posting it as an answer, some moderator might not like it and may give you a down vote. Anyway, I am hoping that you got your indention right. You need to declare where your variable file_data
Can I ask you how call the subprocess from javascript /django?

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.