├── requirements.txt ├── .gitignore ├── package.json ├── ssr_example.js ├── py_vue_ssr.py └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | py-mini-racer==0.1.13 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | env/ 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "py_vue_ssr", 3 | "version": "1.0.0", 4 | "description": "Vue SSR in Python", 5 | "main": "ssr_example.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "vue": "^2.5.16", 13 | "vue-server-renderer": "^2.5.16" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ssr_example.js: -------------------------------------------------------------------------------- 1 | const vm = new Vue({ 2 | template: `
{{ msg }}
`, 3 | data: { 4 | msg: 'Vue SSR in Python' 5 | } 6 | }); 7 | 8 | function renderApp(app) { 9 | 10 | let html 11 | // exposed by `vue-server-renderer/basic.js` 12 | renderVueComponentToString(app, (err, res) => { 13 | html = res 14 | }) 15 | return html 16 | } 17 | 18 | var result = renderApp(vm) 19 | -------------------------------------------------------------------------------- /py_vue_ssr.py: -------------------------------------------------------------------------------- 1 | from py_mini_racer import py_mini_racer 2 | 3 | def read_in_file(path): 4 | with open(path, mode='r') as f: 5 | return f.read() 6 | 7 | 8 | def main(): 9 | ctx = py_mini_racer.MiniRacer() 10 | ctx.eval('var process = { env: { VUE_ENV: "server", NODE_ENV: "production" }}; this.global = { process: process };') 11 | ctx.eval(read_in_file('./node_modules/vue/dist/vue.js')) 12 | ctx.eval(read_in_file('./node_modules/vue-server-renderer/basic.js')) 13 | ctx.eval(read_in_file('./ssr_example.js')) 14 | result = ctx.eval("result") 15 | 16 | print(result) 17 | 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vue SSR in Python 2 | 3 | 4 | This is a simple proof of concept used to illustrate how we can use the Python and the V8 engine via [PyMiniRacer](https://github.com/sqreen/PyMiniRacer) to perform server side rendering of [Vue](https://vuejs.org/) applications from a Python process. This POC was inspired by the [non-Node](https://ssr.vuejs.org/en/non-node.html) documentation you can find for Vue ssr. 5 | 6 | Moving forward I plan to build a proper library for rendering apps within a web framework such as [Flask](https://github.com/pallets/flask) or [APIstar](https://github.com/encode/apistar) 7 | 8 | ## Instructions 9 | 10 | Download the git: 11 | 12 | ``` 13 | $ git clone https://github.com/androiddrew/py_vue_ssr_poc.git 14 | $ cd ./py_vue_ssr_poc 15 | ``` 16 | 17 | Setup a virtual environment and activate it: 18 | 19 | ``` 20 | $ python3 -m venv env 21 | $ source env/bin/activate 22 | ``` 23 | 24 | install your requirements: 25 | 26 | ``` 27 | $(env) pip install -r requirements.txt 28 | $(env) npm install 29 | ``` 30 | 31 | Execute the script: 32 | 33 | ``` 34 | $(env) python py_vue_ssr.py 35 | ``` 36 | 37 | 38 | You will see that our component defined in `ssr_example.js` has been rendered to an HTML string and printed to the console: 39 | 40 | ``` 41 |
Vue SSR in Python
` 42 | ``` 43 | --------------------------------------------------------------------------------