I have a frontend web application built with JavaScript, HTML, and CSS. I would like to incorporate the output of a Python script into my frontend without using a server or a full backend solution.
The Python script is a simple neural network, and I want to display its output in the frontend, such as showing the result as a header or plotting a graph generated by the script.
I have already tried printing the output to the console from the Python script, but I need assistance with transferring that output to the frontend.
Is it possible to achieve this without a server? I am open to suggestions and starting with a simpler solution, like plotting a graph generated by the Python script in the frontend.
I appreciate any guidance or code examples on how to accomplish this. Thank you!
I have looked into Django but I feel as if that seems too complex and more server focused for the simple task I require.
4
Answers
looks like you are trying to make a web application with no backend.
javascript is your only hope, if there is no backend.
it is not possible to integrate python with CSS, Javascript, and HTML without a backend.
Yes, it’s possible to incorporate the output of a Python script into your frontend without a server or a full backend solution. One way to achieve this is by using a tool like WebAssembly to run your Python script directly in the browser.WebAssembly is a low-level bytecode format that enables running code written in a variety of languages, including Python, directly in the browser. By using WebAssembly, you can compile your Python script into a format that can be executed in the browser without the need for a server.
There are several ways to compile Python code to WebAssembly, such as using tools like Pyodide or PyO3. Once you have compiled your Python script to WebAssembly, you can then use JavaScript to load and execute the WebAssembly module in your frontend.
Here’s an example of how you can use WebAssembly to plot a graph generated by a Python script in the frontend:
First, you’ll need to compile your Python script to WebAssembly using a tool like Pyodide. You can follow the Pyodide documentation to learn how to do this.
Once you have your WebAssembly module, you can load it in your frontend using JavaScript. Here’s an example of how you can load the module:
In this code, i created a scatter plot with the x-axis values fixed to
[6, 2, 3, 1, 5]
and the y-axis values set to the output of the Python script. You should customize the plot data and layout to suit your needs.Note that this approach requires modern browser support for WebAssembly and may not be compatible with all browsers. Additionally, WebAssembly is a low-level format that requires some knowledge of binary code and memory management. If you’re not comfortable with these concepts, you may want to consider using a higher-level solution like a server or a full backend framework like Flask, Django or others…
If you want to incorporate the output of a Python script into your frontend without using a server or a full backend solution, you have a few options available. One approach is to use a technique called "WebAssembly" (Wasm) to run the Python script directly in the browser.
Emscripten: https://emscripten.org/
Pyodide: https://github.com/pyodide/pyodide
Looks like the same problem we solved by using the python package streamlit. Streamlit is a Python package that can be installed using
pip install streamlit
. You run your scripts from command line usingstreamlit run <python-scipt>
.You can also deploy streamlit on a server to serve your python scripts e.g. within a company network.