36 | );
37 | }
38 |
39 | export default App;
40 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
33 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting started with pyodide in React
2 |
3 | This repository serves as a template for using pyodide in React. It was created using `create-react-app`.
4 |
5 | ## Quickstart
6 |
7 | To get started, simply run the lines below in your terminal:
8 | ```
9 | git clone https://github.com/xhlulu/react-pyodide-template.git
10 | cd react-pyodide-template
11 | npm i
12 | npm start
13 | ```
14 |
15 | When you are ready, you can serve the production build:
16 | ```
17 | npm i -g serve
18 | serve -s build
19 | ```
20 |
21 | ## What just happened
22 |
23 | Now that you ran the app, you saw `5+7=12` being computed after a few seconds.
24 |
25 | What happened behind the scene was: (1) we fetched a python script, (2) we ran it from the JavaScript scope using `pyodide`, and (3) we updated our React component using state hooks.
26 |
27 | Here's the code we just ran:
28 | ```python
29 | def func():
30 | return 5 + 7
31 |
32 | func()
33 | ```
34 |
35 | It's very simple but you can run pretty advanced `python` code once you start loading packages like `numpy`, `pandas`, `scipy`, etc. albeit at the cost of increased loading time.
36 |
37 |
38 | ## What you need to change
39 |
40 | Below are some files you might need to modify for your own project:
41 |
42 | 1. 🐍 [`src/python/script.py`](./src/python/script.py)\
43 | This file contains a toy Python function being defined and run inside your browser using `pyodide`. You can modify this to have more complex use cases (e.g. load standard libraries, `pandas`, `numpy`, etc.). For more details, [read the pyodide docs](https://pyodide.readthedocs.io/en/latest/index.html).
44 |
45 | 2. ⚛️ [`src/App.js`](./src/App.js) \
46 | This file contains the code that reads a `python` script, load `pyodide` and run the script. It also renders a basic React app directly derived from `create-react-app`, which can be easily modified. You will need to modify this file if you want to [access the Python scope from JS](https://pyodide.readthedocs.io/en/latest/usage/quickstart.html#accessing-python-scope-from-javascript) or [load packages](https://pyodide.readthedocs.io/en/latest/usage/loading-packages.html) like `numpy`, `pandas`, etc.
47 |
48 | 3. 📇 [`public/index.html`](./public/index.html)\
49 | The code in the html template was modified to load pyodide inside the ``. If you wish to change the version of `pyodide` being loaded, you will have to modify the loading.
50 |
51 | 4. 🎨 [`src/App.css`](./src/App.css)\
52 | Modify this file or `index.css` in order to add custom styling to your application.
53 |
54 | 5. 🧪 [`src/App.test.js`](./src/App.test.js)\
55 | No test has been set up for this project. You will need to create your own tests and run them with `npm run test`.
56 |
57 |
58 | ## Available Scripts
59 |
60 | In the project directory, you can run:
61 |
62 | ### `npm start`
63 |
64 | Runs the app in the development mode.\
65 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
66 |
67 | The page will reload if you make edits.\
68 | You will also see any lint errors in the console.
69 |
70 |
74 |
75 | ### `npm run build`
76 |
77 | Builds the app for production to the `build` folder.\
78 | It correctly bundles React in production mode and optimizes the build for the best performance.
79 |
80 | The build is minified and the filenames include the hashes.\
81 | Your app is ready to be deployed!
82 |
83 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
84 |
85 | ### `npm run eject`
86 |
87 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
88 |
89 | 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.
90 |
91 | 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.
92 |
93 | 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.
94 |
95 |
96 | ## Acknowledgement
97 |
98 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
99 |
100 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
101 |
102 | To learn React, check out the [React documentation](https://reactjs.org/).
103 |
--------------------------------------------------------------------------------