|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in Python Mini-Projects series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/91_Python_Mini_Projects)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python Program to Run JavaScript in Python" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 1, |
| 22 | + "metadata": { |
| 23 | + "ExecuteTime": { |
| 24 | + "end_time": "2021-07-25T13:56:39.531715Z", |
| 25 | + "start_time": "2021-07-25T13:56:35.493643Z" |
| 26 | + } |
| 27 | + }, |
| 28 | + "outputs": [ |
| 29 | + { |
| 30 | + "name": "stdout", |
| 31 | + "output_type": "stream", |
| 32 | + "text": [ |
| 33 | + "'Running JavaScript in Python'\n", |
| 34 | + "Enter value of x: 6\n", |
| 35 | + "Result: x + 3 = 9\n" |
| 36 | + ] |
| 37 | + } |
| 38 | + ], |
| 39 | + "source": [ |
| 40 | + "'''\n", |
| 41 | + "Python Program to Run JavaScript in Python\n", |
| 42 | + "'''\n", |
| 43 | + "\n", |
| 44 | + "\"\"\"\n", |
| 45 | + "Equivalent Python code:\n", |
| 46 | + "\n", |
| 47 | + "# Example 1: \n", |
| 48 | + "print(\"Running JavaScript in Python\")\n", |
| 49 | + " \n", |
| 50 | + "#Example 2: \n", |
| 51 | + "def add(x, y):\n", |
| 52 | + " return x + y\n", |
| 53 | + " \n", |
| 54 | + "\"\"\"\n", |
| 55 | + "\n", |
| 56 | + "\n", |
| 57 | + "# Import the necessary module!\n", |
| 58 | + "import js2py\n", |
| 59 | + "\n", |
| 60 | + "# Example 1: JavaScript command\n", |
| 61 | + "# Use of eval.js method from js2py module here, \n", |
| 62 | + "# Pass in our js code in it and store it in res1.\n", |
| 63 | + "js_1 = 'console.log(\"Running JavaScript in Python\")'\n", |
| 64 | + "\n", |
| 65 | + "res1 = js2py.eval_js(js_1)\n", |
| 66 | + "\n", |
| 67 | + "# Print the result\n", |
| 68 | + "res1\n", |
| 69 | + "\n", |
| 70 | + "########################################################\n", |
| 71 | + "\n", |
| 72 | + "# Exapmle 2: JavaScript function\n", |
| 73 | + "\n", |
| 74 | + "# Ask for user input\n", |
| 75 | + "x = int(input(\"Enter value of x: \"))\n", |
| 76 | + "js_2 = '''function add(x, y){\n", |
| 77 | + " return x + y;\n", |
| 78 | + " }'''\n", |
| 79 | + "# Use of eval.js method from js2py module here, \n", |
| 80 | + "# Pass in our js code in it and store it in res2. \n", |
| 81 | + "res2 = js2py.eval_js(js_2)\n", |
| 82 | + "\n", |
| 83 | + "# Print the result\n", |
| 84 | + "print(\"Result: x + 3 =\", res2(x,3))" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": null, |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [] |
| 93 | + } |
| 94 | + ], |
| 95 | + "metadata": { |
| 96 | + "hide_input": false, |
| 97 | + "kernelspec": { |
| 98 | + "display_name": "Python 3", |
| 99 | + "language": "python", |
| 100 | + "name": "python3" |
| 101 | + }, |
| 102 | + "language_info": { |
| 103 | + "codemirror_mode": { |
| 104 | + "name": "ipython", |
| 105 | + "version": 3 |
| 106 | + }, |
| 107 | + "file_extension": ".py", |
| 108 | + "mimetype": "text/x-python", |
| 109 | + "name": "python", |
| 110 | + "nbconvert_exporter": "python", |
| 111 | + "pygments_lexer": "ipython3", |
| 112 | + "version": "3.8.8" |
| 113 | + }, |
| 114 | + "toc": { |
| 115 | + "base_numbering": 1, |
| 116 | + "nav_menu": {}, |
| 117 | + "number_sections": true, |
| 118 | + "sideBar": true, |
| 119 | + "skip_h1_title": false, |
| 120 | + "title_cell": "Table of Contents", |
| 121 | + "title_sidebar": "Contents", |
| 122 | + "toc_cell": false, |
| 123 | + "toc_position": {}, |
| 124 | + "toc_section_display": true, |
| 125 | + "toc_window_display": false |
| 126 | + }, |
| 127 | + "varInspector": { |
| 128 | + "cols": { |
| 129 | + "lenName": 16, |
| 130 | + "lenType": 16, |
| 131 | + "lenVar": 40 |
| 132 | + }, |
| 133 | + "kernels_config": { |
| 134 | + "python": { |
| 135 | + "delete_cmd_postfix": "", |
| 136 | + "delete_cmd_prefix": "del ", |
| 137 | + "library": "var_list.py", |
| 138 | + "varRefreshCmd": "print(var_dic_list())" |
| 139 | + }, |
| 140 | + "r": { |
| 141 | + "delete_cmd_postfix": ") ", |
| 142 | + "delete_cmd_prefix": "rm(", |
| 143 | + "library": "var_list.r", |
| 144 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 145 | + } |
| 146 | + }, |
| 147 | + "types_to_exclude": [ |
| 148 | + "module", |
| 149 | + "function", |
| 150 | + "builtin_function_or_method", |
| 151 | + "instance", |
| 152 | + "_Feature" |
| 153 | + ], |
| 154 | + "window_display": false |
| 155 | + } |
| 156 | + }, |
| 157 | + "nbformat": 4, |
| 158 | + "nbformat_minor": 2 |
| 159 | +} |
0 commit comments