简体   繁体   English

仅使用javascript或Django调用python脚本

[英]Call a python script using only javascript or django

I'm using splunk and its framework that works only in javascript and Django. 我正在使用splunk及其仅在javascript和Django中有效的框架。 I would like open an external file and edit it, but with javascript is impossible and I'm not sure that with Django. 我想打开一个外部文件并进行编辑,但是使用javascript是不可能的,并且我不确定使用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. 因此,我以为可以从javascript或Django(例如python)中调用外部脚本,以打开文件进行编辑,然后将结果发送到javascript页面。

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: 要编辑的文件存储在与我的javascript页面不同的子文件夹中,但是它们都在同一个splunk_app文件夹中:

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 谢谢

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: 谢谢您的帮助,我正在尝试使用splunk绑定,因此我准备了以下三个文件:url,views和mypage.html,因此添加了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: 以及mypage.html django代码:

{% 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? 我将使用字典,而不是file_Data,但是如何使用mypage.html中的子进程执行myscript python文件?

Thank you so much for your help. 非常感谢你的帮助。

Splunk is built on the django web framework. Splunk建立在django网络框架上。 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 使用子进程执行脚本python文件
  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/ 在香草django中,假设您已将地址路由到urls.py中的正确视图,则您的views.py函数应如下所示: 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 在您的模板中,您可以通过django模板语法访问它们

{% 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. 另外,splunk提供了一些绑定来执行您的自定义视图。 You can check out http://dev.splunk.com/view/SP-CAAAEMP 您可以查看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 这次您不需要返回渲染,因为splunk装饰器将为您完成@render_to

To do a subprocess, check out http://pymotw.com/2/subprocess/ 要执行子流程,请查看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. 无论哪种方式,由于它是python脚本,因此您只需将代码包装在一个函数中,然后将其导入即可。 For instance, 例如,

in yourscripts.py 在yourscripts.py中

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

and in your view.py do: 并在您的view.py中执行:

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 如果要调用外部python脚本,请执行子过程命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM