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
- Execute your script python file using subprocess
- 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