use python libraries in react native

  • Last Update :
  • Techknowledgy :

In this framework, best way to integrate Python Code, machine learning or otherwise is to connect NodeJS app with Python interpreter. This also happens to be complex to implement. This will go something like this

#include <Python.h>


int main(int argc, char *argv[]){
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                 "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

Suggestion : 2

January 21, 2022 13 min read 3745

Below is an example of a class component:

class Header extends React.Component {
  render() {
    return <h2>Hello, I am a header!</h2>;
  }
}

The equivalent of the class component in Python is as follows:

class MyComponent(Component):
   def __init__(self):
   super().__init__()

def render(self):
   return "Hello, I am a heder";

A functional component, like a class component, returns HTML and operates similarly, but functional components can be constructed with significantly less code, are easier to grasp due to their simple syntax, and are favored in this tutorial:

function Header() {
  return <h2>Hello, I am a header!</h2>;
}

Secondly, in HTML, we use strings like the following to define inline styles:

<h1 style="color: hotpink; font-size: 12px">Hello<h1>

However, in JSX, we utilize camel-cased objects:

<h1 style="color" : "hotpink" , "fontSize" : "12px"> Hello </h1>

Suggestion : 3

Joined Jul 15, 2021 , Posted on Jun 21, 2021 • Updated on Apr 30 , Joined Feb 7, 2021 , Joined Jul 28, 2020

def say_hello():
   document.getElementById('destination').innerHTML = "Hello World!"

def clear_it():
   document.getElementById('destination').innerHTML = ""
<!DOCTYPE html>
<html lang="en">
    <body>
        <script type="module">
            import {say_hello, clear_it} from "./__target__/hello.js";
            document.getElementById("sayBtn").onclick = say_hello;
            document.getElementById("clearBtn").onclick = clear_it;
        </script>
        <button type="button" id="sayBtn">Click Me!</button>
        <button type="button" id="clearBtn">Clear</button>
        <div id="destination"></div>
    </body>
</html>
// Transcrypt'ed from Python
import {
   AssertionError,
   ...,
   zip
} from './org.transcrypt.__runtime__.js';
var __name__ = '__main__';
export var say_hello = function() {
   document.getElementById('destination').innerHTML = 'Hello World!';
};
export var clear_it = function() {
   document.getElementById('destination').innerHTML = '';
};

//# sourceMappingURL=hello.map
def print_stuff():
   console.log("Native JS console.log call")
print("Python print")
console.invalid_method("This will be an error")
<!DOCTYPE html>
<html lang="en">
    <body>
        <script type="module">
            import {print_stuff} from "./__target__/sourcemap.js";
            document.getElementById("printBtn").onclick = print_stuff;
        </script>
        <button type="button" id="printBtn">Print</button>
    </body>
</html>
useState = React.useState
el = React.createElement

def App():
   val, setVal = useState("")

def say_hello():
   setVal("Hello React!")

def clear_it():
   setVal("")

return [
   el('button', {
      'onClick': say_hello
   }, "Click Me!"),
   el('button', {
      'onClick': clear_it
   }, "Clear"),
   el('div', None, val)
]

def render():
   ReactDOM.render(
      el(App, None),
      document.getElementById('root')
   )

document.addEventListener('DOMContentLoaded', render)