Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 2b85b4e

Browse files
authored
Merge pull request #95 from KyleKing/dev/add-CRA-demo
Add Create-React-App Example
2 parents 56b2434 + d657a86 commit 2b85b4e

File tree

19 files changed

+15677
-0
lines changed

19 files changed

+15677
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
*.spec
522 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `npm start`
8+
9+
Runs the app in the development mode.<br>
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br>
13+
You will also see any lint errors in the console.
14+
15+
### `npm test`
16+
17+
Launches the test runner in the interactive watch mode.<br>
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `npm run build`
21+
22+
Builds the app for production to the `build` folder.<br>
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br>
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `npm run eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35+
36+
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# README
2+
3+
Eello World example Create-React-App (CRA) with Eel. This particular project was bootstrapped with `npx create-react-app 07_CreateReactApp --typescript` (Typescript enabled), but the below modifications can be implemented in any CRA configuration or CRA version.
4+
5+
If you run into any issues with this example, open a [new issue](https://github.com/ChrisKnott/Eel/issues/new) and tag @KyleKing
6+
7+
## Quick Start
8+
9+
1. In the app's directory, run `npm install` and `pip install bottle bottle-websocket future whichcraft pyinstaller`
10+
2. Build the application with `npm run build`
11+
3. Run the built version with `python eel_CRA.py`. A Chrome window should open running the code from `build/`
12+
4. Build a binary distribution with PyInstaller using `python -m eel eel_CRA.py build --onefile` (See more detailed instructions at bottom of [the main README](https://github.com/ChrisKnott/Eel))
13+
5. For development, open two prompts. In one, run `python eel_CRA.py true` and the other, `npm start`. A browser window should open in your default web browser at: [http://localhost:3000/](http://localhost:3000/). As you make changes to the JavaScript in `src/` the browser will reload. Any changes to `eel_CRA.py` will require a restart to take effect. You may need to refresh the browser window if it gets out of sync with eel.
14+
15+
![Demo.png](Demo.png)
16+
17+
## About
18+
19+
These are the changes needed to convert the basic CRA application for Eel support.
20+
21+
### Use `window.eel.expose(func, 'func')` with `npm run build`
22+
23+
> TLDR: CRA's default code mangling in `npm run build` will rename variables and functions. To handle these changes, convert all `eel.expose(funcName)` to `window.eel(funcName, 'funcName')`. This workaround guarantees that 'funcName' will be available to call from Python
24+
25+
When you run `npm run build`, CRA generates a mangled and minified JavaScript file. The mangling will change `eel.expose(funcName)` to something like `D.expose(J)`. The modified code won't be recognized by the Eel static JS-code analyzer, which uses a regular expression to look for `eel.expose(*)` and the function name expected in python, `funcName`, is now mangled as `J`.
26+
27+
The easy workaround is just to refactor `eel.expose(funcName)` to `window.eel(funcName, 'func_name')` where `window.eel` prevents `eel` from being mangled. Then `eel.func_name()` can be called from Python
28+
29+
### src/App.tsx
30+
31+
Modified to demonstrate exposing a function from JavaScript and how to use callbacks from Python to update React GUI.
32+
33+
### eel_CRA.py
34+
35+
Basic eel file that exposes two Python functions to JavaScript. If a second argument (i.e. `true`) is provided, the app uses a development mode for a server already alive on port 3000; otherwise, the script will load `index.html` from the build/ directory, which is what you want for building from a binary.
36+
37+
### public/index.html
38+
39+
Added location of `eel.js` file based on options set in eel_CRA.py.
40+
41+
```html
42+
<!-- Load eel.js from the port specified in the eel.start options -->
43+
<script type="text/javascript" src="http://localhost:8080/eel.js"></script>
44+
```
45+
46+
### src/react-app-env.d.ts
47+
48+
This file declares window.eel as a valid type for tslint. Note: capitalization of `window`
49+
50+
51+
### src/App.css
52+
53+
Added some basic button styling :)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
sys.path.insert(1, '../../')
3+
# Use latest version of Eel from parent directory
4+
5+
import os
6+
import random
7+
8+
import eel
9+
10+
11+
@eel.expose # Expose this function to JavaScript
12+
def say_hello_py(x):
13+
# Print to Python console
14+
print('Hello from %s' % x)
15+
# Call a JavaScript function
16+
eel.say_hello_js('Python {from within say_hello_py()}!')
17+
18+
19+
@eel.expose
20+
def pick_file(folder):
21+
folder = os.path.expanduser(folder)
22+
if os.path.isdir(folder):
23+
return random.choice(os.listdir(folder))
24+
else:
25+
return '{} is not a valid folder'.format(folder)
26+
27+
28+
def start_eel(develop):
29+
"""Start Eel with either production or development configuration"""
30+
if develop:
31+
directory = 'src'
32+
app = None
33+
page = {'port': 3000}
34+
flags = ['--auto-open-devtools-for-tabs']
35+
else:
36+
directory = 'build'
37+
app = 'chrome-app'
38+
page = 'index.html'
39+
flags = []
40+
41+
eel.init(directory, ['.tsx', '.ts', '.jsx', '.js', '.html'])
42+
43+
# These will be queued until the first connection is made, but won't be repeated on a page reload
44+
say_hello_py('Python World!')
45+
eel.say_hello_js('Python World!') # Call a JavaScript function (must be after `eel.init()`)
46+
47+
eel.start(page, size=(1280, 800), options={
48+
'mode': app,
49+
'port': 8080,
50+
'host': 'localhost',
51+
'chromeFlags': flags
52+
})
53+
54+
if __name__ == '__main__':
55+
import sys
56+
57+
# Pass any second argument to enable debugging. Production distribution can't receive arguments
58+
start_eel(develop=len(sys.argv) == 2)

0 commit comments

Comments
 (0)