-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Update JS function name parsing Use a custom PyParsing grammar to extract function names from JavaScript names rather than a basic and somewhat flawed regex pattern.
- Loading branch information
Showing
13 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { Component } from 'react'; | ||
import logo from './logo.svg'; | ||
import './App.css'; | ||
|
||
// Point Eel web socket to the instance | ||
export const eel = window.eel | ||
eel.set_host( 'ws://localhost:8080' ) | ||
|
||
// Expose the `sayHelloJS` function to Python as `say_hello_js` | ||
function sayHelloJS( x: any ) { | ||
console.log( 'Hello from ' + x ) | ||
} | ||
// WARN: must use window.eel to keep parse-able eel.expose{...} | ||
window.eel.expose( sayHelloJS, 'say_hello_js' ) | ||
|
||
// Test anonymous function when minimized. See https://github.com/samuelhwilliams/Eel/issues/363 | ||
function show_log(msg:string) { | ||
console.log(msg) | ||
} | ||
window.eel.expose(show_log, 'show_log') | ||
|
||
// Test calling sayHelloJS, then call the corresponding Python function | ||
sayHelloJS( 'Javascript World!' ) | ||
eel.say_hello_py( 'Javascript World!' ) | ||
|
||
// Set the default path. Would be a text input, but this is a basic example after all | ||
const defPath = '~' | ||
|
||
interface IAppState { | ||
message: string | ||
path: string | ||
} | ||
|
||
export class App extends Component<{}, {}> { | ||
public state: IAppState = { | ||
message: `Click button to choose a random file from the user's system`, | ||
path: defPath, | ||
} | ||
|
||
public pickFile = () => { | ||
eel.pick_file(defPath)(( message: string ) => this.setState( { message } ) ) | ||
} | ||
|
||
public render() { | ||
eel.expand_user(defPath)(( path: string ) => this.setState( { path } ) ) | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p>{this.state.message}</p> | ||
<button className='App-button' onClick={this.pickFile}>Pick Random File From `{this.state.path}`</button> | ||
</header> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{% extends 'base.html' %} | ||
{% block title %}Hello, World!{% endblock %} | ||
{% block head_scripts %} | ||
eel.expose(say_hello_js); // Expose this function to Python | ||
function say_hello_js(x) { | ||
console.log("Hello from " + x); | ||
} | ||
|
||
eel.expose(js_random); | ||
function js_random() { | ||
return Math.random(); | ||
} | ||
|
||
function print_num(n) { | ||
console.log('Got this from Python: ' + n); | ||
} | ||
|
||
eel.py_random()(print_num); | ||
|
||
say_hello_js("Javascript World!"); | ||
eel.say_hello_py("Javascript World!"); // Call a Python function | ||
{% endblock %} | ||
{% block content %} | ||
Hello, World! | ||
<br /> | ||
<a href="page2.html">Page 2</a> | ||
{% endblock %} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!-- Below is from: https://github.com/samuelhwilliams/Eel/blob/master/examples/05%20-%20input/web/main.html --> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous"> | ||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> | ||
<!-- Include eel.js - note this file doesn't exist in the 'web' directory --> | ||
<script type="text/javascript" src="/eel.js"></script> | ||
<script type="text/javascript"> | ||
$(function(){ | ||
|
||
eel.expose(say_hello_js); // Expose this function to Python | ||
function say_hello_js(x) { | ||
console.log("Hello from " + x); | ||
} | ||
say_hello_js("Javascript World!"); | ||
eel.handleinput("connected!"); // Call a Python function | ||
|
||
$("#btn").click(function(){ | ||
eel.handleinput($("#inp").val()); | ||
$('#inp').val(''); | ||
}); | ||
}); | ||
</script> | ||
</head> | ||
|
||
<body> | ||
<h1>Input Example: Enter a value and check python console</h1> | ||
<div> | ||
<input type='text' id='inp' placeholder='Write anything'> | ||
<input type='button' id='btn' class='btn btn-primary' value='Submit'> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import eel | ||
import pytest | ||
from tests.utils import TEST_DATA_DIR | ||
|
||
# Directory for testing eel.__init__ | ||
INIT_DIR = TEST_DATA_DIR / 'init_test' | ||
|
||
|
||
@pytest.mark.parametrize('js_code, expected_matches', [ | ||
('eel.expose(w,"say_hello_js")', ['say_hello_js']), | ||
('eel.expose(function(e){console.log(e)},"show_log_alt")', ['show_log_alt']), | ||
(' \t\nwindow.eel.expose((function show_log(e) {console.log(e)}), "show_log")\n', ['show_log']), | ||
((INIT_DIR / 'minified.js').read_text(), ['say_hello_js', 'show_log_alt', 'show_log']), | ||
((INIT_DIR / 'sample.html').read_text(), ['say_hello_js']), | ||
((INIT_DIR / 'App.tsx').read_text(), ['say_hello_js', 'show_log']), | ||
((INIT_DIR / 'hello.html').read_text(), ['say_hello_js', 'js_random']), | ||
]) | ||
def test_exposed_js_functions(js_code, expected_matches): | ||
"""Test the PyParsing PEG against several specific test cases.""" | ||
matches = eel.EXPOSED_JS_FUNCTIONS.parseString(js_code).asList() | ||
assert matches == expected_matches, f'Expected {expected_matches} (found: {matches}) in: {js_code}' | ||
|
||
|
||
def test_init(): | ||
"""Test eel.init() against a test directory and ensure that all JS functions are in the global _js_functions.""" | ||
eel.init(path=INIT_DIR) | ||
result = eel._js_functions.sort() | ||
functions = ['show_log', 'js_random', 'show_log_alt', 'say_hello_js'].sort() | ||
assert result == functions, f'Expected {functions} (found: {result}) in {INIT_DIR}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters