Splunk Dev

pass xml param to python module

Benomran
Explorer

In the documentations, it shows how you can send param value to your java script (js) file but it does not show how to send to python file within the same module. I tried playing around with generateResults but got nowhere. Unless I missed something, I am lost.

Examples 7 had something like this, but is limited to only 4 arguments:

def generateResults(self, host_app, client_app, sid, count=1000, offset=0, entity_name='results'):

I already included my param in the conf file:

[param:startsize]
required = False
default = 8
label = This parameter defines start scale size.

[param:endsize]
required = False
default = 70
label = This parameter defines end scale size.

What is the proper way to pull the parameter value into python?

0 Karma
1 Solution

ziegfried
Influencer

The parameters the python endpoint receives are sent by the javascript part of the module. You can override the generation of this parameter list:

<app>/appserver/module/MyFunkyModule/MyFunkyModule.js

Splunk.Module.MyFunkyModule = $.klass(Splunk.Module.DispatchingModule, {
   ...
    getResultParams: function($super) {
        var params = $super();
        params['startsize'] = this.getParam('startsize');
        params['endsize'] = this.getParam('endsize');
        return params;
    }
   ...
}

then you'll be able to use it in python:

<app>/appserver/module/MyFunkyModule/MyFunkyModule.py

def generateResults(self, host_app, client_app, sid, count=1000, offset=0, entity_name='results', startsize, endsize):

View solution in original post

ziegfried
Influencer

The parameters the python endpoint receives are sent by the javascript part of the module. You can override the generation of this parameter list:

<app>/appserver/module/MyFunkyModule/MyFunkyModule.js

Splunk.Module.MyFunkyModule = $.klass(Splunk.Module.DispatchingModule, {
   ...
    getResultParams: function($super) {
        var params = $super();
        params['startsize'] = this.getParam('startsize');
        params['endsize'] = this.getParam('endsize');
        return params;
    }
   ...
}

then you'll be able to use it in python:

<app>/appserver/module/MyFunkyModule/MyFunkyModule.py

def generateResults(self, host_app, client_app, sid, count=1000, offset=0, entity_name='results', startsize, endsize):

jleduc
Explorer

you have to set your parameters before the ones that have a default value: count, offset...

otherwise you won't be able to start the web service:
with this error:
def generateResults(self, host_app, client_app, sid, count=1000,
SyntaxError: non-default argument follows default argument

So here is what you should use now:
def generateResults(self, host_app, client_app, sid, startsize, endsize, count=1000, offset=0, entity_name='results')

0 Karma

Benomran
Explorer

Thank you! I Got it to work with minor adjustment. 😉

For those who may run into this I am including additional information I encountered;
Error: [non-default argument follows default argument]
Fix: Set defaults...

def generateResults(self, host_app, client_app, sid, count=1000, offset=0, entity_name='results', startsize=8, endsize=70):

0 Karma

Benomran
Explorer

Sorry was not clear; like many modules (example 6), they contain a python file which renders the output. My python displays a div tag and calls in a third-party JavaScript. While calling the JavaScript, I would like to modify size value from my XML file which will pass it on to my python file.

Hope that explains what I am trying to achieve!

Thank you

0 Karma

Ayn
Legend

Modules are created in Javascript, not Python. What Python module are you referring to? What are you trying to achieve?

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...