{"id":20954047,"url":"https://github.com/megasanjay/electron-flask","last_synced_at":"2025-05-14T04:32:17.329Z","repository":{"id":39792711,"uuid":"379371470","full_name":"megasanjay/electron-flask","owner":"megasanjay","description":"Using Flask as the backend of an Electron.js application","archived":false,"fork":false,"pushed_at":"2023-12-15T20:29:09.000Z","size":23,"stargazers_count":11,"open_issues_count":4,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T12:48:46.495Z","etag":null,"topics":["electron","electron-builder","flask","pyinstaller"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/megasanjay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-22T18:53:49.000Z","updated_at":"2024-01-27T15:29:25.000Z","dependencies_parsed_at":"2022-09-09T00:52:05.443Z","dependency_job_id":null,"html_url":"https://github.com/megasanjay/electron-flask","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megasanjay%2Felectron-flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megasanjay%2Felectron-flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megasanjay%2Felectron-flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megasanjay%2Felectron-flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/megasanjay","download_url":"https://codeload.github.com/megasanjay/electron-flask/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254071307,"owners_count":22009769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["electron","electron-builder","flask","pyinstaller"],"created_at":"2024-11-19T01:06:11.980Z","updated_at":"2025-05-14T04:32:17.056Z","avatar_url":"https://github.com/megasanjay.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Requirements: \nIf we are using the conda environment add flask to the environment file.\n```bash\npip install flask\npip install pyinstaller\n\n# for windows only\npip install pypiwin32 # for pyinstaller\n```\n\n## renderer.js\n```js\nconst axios = require(\"axios\");\n\nconst SERVER_URL = \"http://127.0.0.1:5000\";\n\n// Check server connection\n(() =\u003e {\n  axios\n    .get(`${SERVER_URL}/`)\n    .then((response) =\u003e {\n      console.log(response.data);\n    })\n    .catch((error) =\u003e {\n      console.error(error);\n    });\n})();\n\nconst requestObject = {\n  key: \"Hello world from Electron!\",\n};\n\nconst echo = (requestObject) =\u003e {\n  axios\n    .post(`${SERVER_URL}/echo`, requestObject)\n    .then((response) =\u003e {\n      console.log(\"Server response: \", response.data);\n    })\n    .catch((error) =\u003e {\n      console.error(error);\n    });\n};\n```\nUsing the axios library, the backend will behave just like a regular web server. Use `axios.get()`, `axios.post()`, etc... to send the requests to the api. The `SERVER_URL` will remain constant and the port will be whatever is defined in `main.js`. If you need timeouts set the appropriate timeouts directly in the axios request.\n\n## api.py\n\n```python\nfrom __future__ import print_function\nfrom flask import Flask, request, jsonify\nfrom calc import calc as real_calc\nimport time\n\napp = Flask(__name__)\n\n@app.route('/', methods=[\"GET\"])\ndef hello():\n    return \"Server active!\"\n\n@app.route('/echo', methods=[\"POST\"])\ndef echo():\n    try:\n        data = request.json\n        print(\"Server: \", data[\"key\"])\n        return \"Hello World from Server!\"\n    except Exception as e:\n        raise e\n\n# :5000 is the flask default port. \n# You can change it to something else if you would like.\nif __name__ == '__main__':\n    app.run(host='127.0.0.1', port=5000)\n```\nYou can use this template to start. All the routes are defined under the `@app.route(\u003cendpoint\u003e, methods=[\"method\"])` syntax. Keep the function name inside the route descriptive. Use only one `def` inside the route and for any additional functions, declare/define them outside the `@app` block.\n\nThe return type must be a string, dict, tuple, Response instance, or WSGI callable (NO INT RETURNS)\n\n## For quick testing \n\nIf you don't want to have to create the python process everytime, use the following instructions to have a more realtime view of the program. Set the `createPyProc` function to not run.\n\n```js\n// main.js\n\nconst createPyProc = () =\u003e {\n  return;\n}\n```\nOpen the flask backend on the terminal window. You will be able to see all the requests that come through. If you set any print statements in your python output, they will be visible here as well. To run this:\n```bash\npython ./api.py\n```\n\n## Packaging and distribution\n\nEverything here works exactly the same as [SODA](https://github.com/bvhpatel/SODA/wiki/Packaging). On Windows, I didn't need to move any file or folders into the package. Should be a drop in replacement. You will need to add the `npm run` scripts for this repo.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegasanjay%2Felectron-flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegasanjay%2Felectron-flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegasanjay%2Felectron-flask/lists"}