Skip to content

Commit

Permalink
Hotfix for Python trasnformation (ToolJet#4641)
Browse files Browse the repository at this point in the history
* fixes: correct state

* undo

* updates with correct state

* handle pythong comments

* fixes: should also update the options when language is updated onMount

* handle variables when undefined
  • Loading branch information
arpitnath authored Oct 28, 2022
1 parent 2c2ffd7 commit b3cb665
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 6 additions & 2 deletions frontend/src/Editor/QueryManager/Transformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ return [row for row in data if row['amount'] > 1000]
}

useEffect(() => {
changeOption('transformationLanguage', lang);
changeOption('transformation', state[lang]);
if (lang !== options.transformationLanguage) {
changeOption('transformationLanguage', lang);
changeOption('transformation', state[lang]);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lang]);

useEffect(() => {
if (options.enableTransformation) {
changeOption('transformationLanguage', lang);
setState({ ...state, [lang]: options.transformation });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/_helpers/appUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,27 @@ async function exceutePycode(payload, code, currentState, query, mode) {
const evaluatePython = async (pyodide) => {
let result = {};
try {
const _code = code.replace('return', '');
//remove the comments from the code
let codeWithoutComments = code.replace(/#.*$/gm, '');
codeWithoutComments = codeWithoutComments.replace(/^\s+/g, '');
const _code = codeWithoutComments.replace('return ', '');
currentState['variables'] = currentState['variables'] ?? {};
const _currentState = JSON.stringify(currentState);

let execFunction = await pyodide.runPython(`
from pyodide.ffi import to_js
import json
def exec_code(payload):
def exec_code(payload, _currentState):
data = json.loads(payload)
currentState = json.loads('${_currentState}')
currentState = json.loads(_currentState)
components = currentState['components']
queries = currentState['queries']
globals = currentState['globals']
variables = currentState['variables']
client = currentState['client']
server = currentState['server']
code_to_execute = ${_code}
try:
res = to_js(json.dumps(code_to_execute))
# convert dictioanry to js object
Expand All @@ -116,7 +122,7 @@ async function exceutePycode(payload, code, currentState, query, mode) {
exec_code
`);
const _data = JSON.stringify(payload);
result = execFunction(_data);
result = execFunction(_data, _currentState);
return JSON.parse(result);
} catch (err) {
console.error(err);
Expand Down

0 comments on commit b3cb665

Please sign in to comment.