Hi, I'm K Om Senapati, a B Tech CSE student at OUTR, Bhubaneswar, and a Pythonista passionate about hackathons, teamwork, and exploring new technologies.
Web Developer, Freelance Technical Writer, Casual Deep Learning Enjoyer, always eager to work with new technologies & documenting them. Email me for collaboration.
Location
West Bengal, India
Education
Maulana Abul Kalam Azad University of Technology, W.B.
Well actually Jinja really serves its purpose greatly. That being said, it is a templating engine for python.
Jinja is generally static, (HTML rendered before delivery). While JSX is dynamic and is loved by devs mostly for its component-based reusability, nesting and the ability of easily passing props for dynamic behavior.
The best way is to use the best of both worlds.
As in the end, we are converting PyJSX into strings currently for rendering as html, we will be able to use them directly in flask routes. What we can do is break our whole UI into reusable components and then serve them as html when sending via response in flask routes.
A Quick Example for you if my explanation above was messy (sorry 🥲😅😅🥲) -
PyJSX with Flask
With PyJSX, you define your components in Python files (often with a .px extension, though not strictly required for simple cases if transpiled correctly). The output of a PyJSX component is of JSX type which can be formatted in fstrings, which Flask can directly return.
First, define a PyJSX component. You would typically save this in a file, for example, component.px -
Then, in your Flask application (app.py), you would import and use these components:
fromflaskimportFlaskfrompyjsximportauto_setupfromcomponentimportHomePageapp=Flask(__name__)@app.route('/')defhome_pyjsx():# Render the PyJSX component to a string
rendered_html=HomePage(user_name="KOmSenpati",items=["Pen","Paper","Laptop"])print(rendered_html)returnf'{rendered_html}',{'Content-Type':'text/html'}if__name__=='__main__':app.run()
See it works perfectly fine -
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Wow its so cool
But is it better than Jinja2 ?
That can be setup easily with fastapi or flask
Well actually Jinja really serves its purpose greatly. That being said, it is a templating engine for python.
Jinja is generally static, (HTML rendered before delivery). While JSX is dynamic and is loved by devs mostly for its component-based reusability, nesting and the ability of easily passing props for dynamic behavior.
The best way is to use the best of both worlds.
As in the end, we are converting PyJSX into strings currently for rendering as html, we will be able to use them directly in flask routes. What we can do is break our whole UI into reusable components and then serve them as html when sending via response in flask routes.
A Quick Example for you if my explanation above was messy (sorry 🥲😅😅🥲) -
PyJSX with Flask
With PyJSX, you define your components in Python files (often with a .px extension, though not strictly required for simple cases if transpiled correctly). The output of a PyJSX component is of JSX type which can be formatted in fstrings, which Flask can directly return.
First, define a PyJSX component. You would typically save this in a file, for example,
component.px-Then, in your Flask application (
app.py), you would import and use these components:See it works perfectly fine -