PyScript in Odoo
Using PyScript in Odoo
- Create an empty module
- Create template for new page
- Create controller for our page
- Create a new menu in website
Create an empty module
Create a directory named pyscript_base. This will be our Odoo app
Inside the directory create two python files [ __init__.py and __manifest__.py ]
Copy the contents for both files from here
pyscript_base/__init__.py
# -*- coding: utf-8 -*-
from . import controllers
pyscript_base/__manifest__.py
# -*- coding: utf-8 -*-
{
'name': "PyScript Base",
'version': '15.0.1.0',
'summary': """Sample pyscript to Odoo website""",
'description': """
Examples
-Added print functionality
""",
'author': "Mohammed Amal N",
'category': 'Website/Website',
# any module necessary for this one to work correctly
'depends': ['website'],
'data': [
'views/pyscript_template.xml',
'views/website_menu.xml',
],
'application': False,
}
Create a template for our new page
Create a new directory inside our app named views. Here we'll add a template for our new webpage.
create an xml file named pyscript_template
pyscript_base/views/pyscript_template.xml
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<template id="pyscript_template">
<t t-call="website.layout">
<xpath expr="//header" position="inside">
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer="defer" src="https://pyscript.net/alpha/pyscript.js"></script>
</xpath>
<t t-set="title">PyScript</t>
<div class="oe_structure">
<div class="container">
<h1><py-script> print('PyScript Test!') </py-script></h1>
<b><p>Today is <u><label id='today'></label></u></p></b>
<br/>
<div id="pi" class="alert alert-primary"></div>
<py-script>
import datetime as dt
pyscript.write('today', dt.date.today().strftime('%A %B %d, %Y'))
def wallis(n):
pi = 2
for i in range(1,n):
pi *= 4 * i ** 2 / (4 * i ** 2 - 1)
return pi
pi = wallis(100000)
pyscript.write('pi', f'π is approximately {pi:.3f}')
</py-script>
<p>Be Patient</p>
</div>
</div>
<!--<xpath expr="//body" position="replace">-->
<!--<body class="main-body app sidebar-mini"></body>-->
<!--</xpath>-->
</t>
</template>
</odoo>
Currently your app should look like this:
pyscript_base
- views
- pyscript_template.xml
- __init__.py
- __manifest__.py
Create a controller for our page
pyscript_base/controller/__init__.py
# -*- coding: utf-8 -*-
from . import pyscript_controller
pyscript_base/controller/pyscript_controller.py
# -*- coding: utf-8 -*-
from odoo import http
class Academy(http.Controller):
@http.route('/pyscript/', auth='public', website=True)
def pyscript_page(self, **kw):
return http.request.render('pyscript_base.pyscript_template')
This will let you open our page from your_domain.com/pyscript or if you are running locally localhost:8069/pyscript
Your current app structure should be
pyscript_base
- controllers
- __init__.py
- pyscript_controller.py
- views
- pyscript_template.xml
- __init__.py
- __manifest__.py
Congrats, you have created an Odoo web page with pyscript
Final step is to create a website menu for this page. You can skip the final step if you don't need it
Note: If you are not creating a new website menu for our page, you should remove 'views/website_menu.xml' from our manifest file
Create a new menu in website
Our final step is to create website menu to view our page. Create a new xml file named website_menu in views directory
pyscript_base/views/website_menu.xml
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="menu_pyscript" model="website.menu">
<field name="name">PyScript</field>
<field name="url">/pyscript</field>
<field name="parent_id" ref="website.main_menu"/>
<field name="sequence" type="int">20</field>
</record>
</odoo>
Our final app structure should look like:
pyscript_base
- controllers
- __init__.py
- pyscript_controller.py
- views
- pyscript_template.xml
- website_menu.xml
- __init__.py
- __manifest__.py
Our page will look like this
You can find complete code for the app from Github

Comments
Post a Comment