Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4397

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T11:35:09+00:00 2024-11-26T11:35:09+00:00

How the React Pattern Applies Outside of JS: A Case Study

  • 61k

Looking from an oblique angle, React solves a peculiar problem: how to make a static markup language act dynamic. Judging by React's popularity, most of us would agree that React does a particularly good job solving this problem, and code using this ideology of component rendering often leads to self-contained reusable modules.

As an ex-web developer, I always prefer React over any other framework. However, the general design patterns have a much broader use case than solely web development.

The Problem I was Facing

For quite an extensive amount of time, I have been working with Deepmind's simulation software Multi-Joint dynamics with Contact, or MuJoCo for short. For our argument, it does not matter too much what exactly MuJoCo is. We will only focus on its scene definition. By design, simulated environments are compiled from XML files, which define the layout of the simulated scene and its objects.

Just to have an idea, you can find below a minimal example with two cubes, a sphere, and a ground plane.

<mujoco model="simple_scene">     <worldbody>         <!-- Ground plane -->         <geom name="ground" type="plane" size="10 10 0.1"/>          <!-- Red box with 6DOF joint -->         <body name="box_one" pos="0 0 0.1">             <freejoint />             <geom name="box_geom" type="box" size="0.1 0.1 0.1"/>         </body>          <!-- Blue box with 6DOF joint -->         <body name="box_two" pos="0 0 0.5">             <freejoint />             <geom name="box_geom" type="box" size="0.1 0.1 0.1"/>         </body>          <!-- Small sphere -->         <body name="small_sphere" pos="0 0.5 0.1">             <geom name="sphere_geom" type="sphere" size="0.05"/>         </body>          <!-- would be nice to add other objects -->     </worldbody> </mujoco> 
Enter fullscreen mode Exit fullscreen mode

This scene is particularly boring as not much will happen, but the parallel to HTML should be quite obvious. The problem arises when one wants something more dynamic with a variable number of objects. In my case, I had to dynamically exchange robot grippers and random objects throughout various scenes.

So, my goal was to recreate the basics of React for better handling.

It turned out much simpler than I previously imagined.

The Basics of React

At a high level, React provides a simple framework with a custom syntax called JSX to specify components. Components are self-contained units that maintain their own logic and markup. Components can also consist of other components, thus creating a hierarchy.

This creates a tree structure that mimics the actual Document Object Model (DOM) in the browser. To generate the final HTML, React uses a “render” method on the root component, which recursively calls the “render” method on all nested components.

Of course, modern React is much more complex than this, but this covers the basics and is all we need.

Mimicking React in Python

MuJoCo is written in C/C++ and provides native Python bindings for quick prototyping. As I did not plan to recreate JSX inside of Python, I simply opted for the multiline string using placeholders.

In the example MuJoCo scene above, this would translate to:

XML = """ <mujoco model="simple_scene">     <worldbody>         {objects}     </worldbody> </mujoco> """ 
Enter fullscreen mode Exit fullscreen mode

Fortunately, Python comes with a string formatting convention, allowing us to use named placeholders ({objects} in this example) for future replacement using the format(object="string for replacement") method call on the template string.

This looks promising.

Similarly, we can create an XML template for the objects. Here is an example template string that allows for custom size settings for a box object:

XML = """ <body name="{name}" pos="0 0 0.1">     <freejoint />     <geom name="box_geom" type="box" size="{size}"/> </body> """ 
Enter fullscreen mode Exit fullscreen mode

Next, we need an interface for each component to make them share a method required for rendering each component:

from abc import ABC, abstractmethod  class MjXML(ABC):      @abstractmethod     def to_xml(self) -> str:         pass 
Enter fullscreen mode Exit fullscreen mode

In the final XML assembly step, we will call on each component the to_xml method, which will output the resulting string for each component that will be used in the template.

Next, we implement the corresponding component for the example object above:

XML = """ <body name="{name}" pos="0 0 0.1">     <freejoint />     <geom name="box_geom" type="box" size="{size}"/> </body> """  class SimpleBox(MjXML):     def __init__(self, name: str, size: float):         self.name = name         self.size = size      def to_xml(self):         box_xml = XML.format(name=self.name, size=self.size)         return box_xml 
Enter fullscreen mode Exit fullscreen mode

Finally, we implement the component for the scene:

XML = """ <mujoco model="simple_scene">     <worldbody>         {objects}     </worldbody> </mujoco> """  class SimpleScene(MjXML):     def __init__(self, object_list: List[MjXML]):         self.object_list = object_list      def to_xml(self):         obj_xml = " ".join([obj.to_xml() for obj in self.object_list])         full_xml = XML.format(objects=obj_xml)         return full_xml 
Enter fullscreen mode Exit fullscreen mode

Essentially, this is already a minimal workable example.

To actually simulate, we would need to construct the model and data of MuJoCo using our constructed XML string. As this article is not about MuJoCo, I have excluded the simulation part from this code sample.

Possible Extensions

While this example covers the basic concept, my specific use case required additional features such as dynamic loading of assets, randomizing names, and custom collision logic. Let me know, if you are interested in more complex physical simulations.

However, these additional features might overcomplicate the core message I want to convey: if you are working with static markup that needs dynamic behavior, consider using a component-based rendering mechanism similar to React. It can simplify development and enhance the flexibility of your applications.

pythonreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.