Error: Attempting to import statement outside of module when calling JavaScript from Python (Dash)

I am attempting to incorporate a three.js example from the fireship-io YouTube channel to create an animated background on my Python dash application based on user scrolling. The JavaScript script continuously calls a function to update object rotation and camera position.

Although I have successfully run the JavaScript script on its own using node, I encounter an error when trying to call it from my Python scripts, specifically at line 1:

Uncaught SyntaxError: Cannot use import statement outside a module (at main.js?m=1658899110.9005847:1:1)
. While the dash app can access the CSS file because the font is correctly rendered, it is unable to import style.css.

How can I resolve this error? Is there a way to specify "type":"module" in Dash when executing JavaScript?

Note: The JavaScript file is automatically called by Dash as it resides in the assets directory. I have added

"type":"module"
in package.json, but the error persists.

main.js:

import './style.css';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

...

app.py:

app = Dash(__name__)
server = app.server


app.layout = html.Div(children=[
 ...
])


if __name__ == '__main__':
    app.run_server(debug=False)

package.json:

{
  "name": "three-demo",
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview"
  },
  "devDependencies": {
    "vite": "^2.3.0"
  },
  "dependencies": {
    "three": "^0.128.0"
  }
}

Root folder:

app.py
assets/
  - main.js
  - package.json
  - style.css

Answer №1

After reviewing this resource: Enhancing Dash Apps with Your Own CSS and JavaScript, it seems that Dash automatically includes your CSS files if they are in the correct folder.

Therefore, it may be unnecessary to import them in the main.js file.

If you revisit the same link and navigate to the 'Adding External CSS/JavaScript' section, you could experiment with not importing in the main.js file and instead adding this to your python script:

external_scripts = [
  'https://threejs.org/build/three.js'
]

A similar approach may be required for incorporating 'OrbitControls'.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Align Horizontal Fixed Element

<html> <head> <style> #rectangle { position: absolute; right: 0; bottom: 0; width: 150px; height: 200px; } </st ...

Unanticipated commitments fulfilled on the spot

While troubleshooting a setup for managing promise concurrency in handling CPU-intensive asynchronous tasks, I encountered perplexing behavior that has left me puzzled. To regulate the flow, my approach was to initially create an array of Promises and the ...

Retrieving a JavaScript variable from a Python Selenium request

Currently, I am utilizing Python in conjunction with Selenium to monitor changes on our pbx system. A specific value that I require is being retrieved through a JavaScript call and unfortunately, it is not directly written into the HTML code. This has made ...

Negatives of utilizing two different UI libraries within a single react project?

I have been contemplating a decision that may be considered unconventional from a technical standpoint. Despite my search efforts, I have not come across any explanation regarding the potential drawbacks of this decision. Imagine creating a React website ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

Invoke the javascript function by referencing the JavaScript file

I'm encountering an issue with two JavaScript files. One file uses the prototype syntax, while the other utilizes jQuery. Unfortunately, they don't seem to work harmoniously together. I've attempted calling the functions within the files usi ...

Click once to open the JS menu, and click again to be redirected to the URL

I am attempting to design a dropdown menu that will reveal the child elements upon first click. When clicking on a child element, it should navigate to the child's URL. Clicking on the country name again should collapse the child menu. Although I hav ...

The combination of sass-loader and Webpack fails to produce CSS output

Need help with setting up sass-loader to compile SCSS into CSS and include it in an HTML file using express.js, alongside react-hot-loader. Check out my configuration file below: var webpack = require('webpack'); var ExtractTextPlugin = require ...

Tips for avoiding double reservations and creating time slots with nextjs and prisma

Welcome to my NextJS app booking system. As a beginner, I'm exploring how to create a website for simple bookings and have successfully connected it to Netlify. Currently, I can gather booking details such as time and name using Netlify forms. Howeve ...

What could be causing the console in my browser to go haywire and crash when I use the applyMatrix function in three

I'm currently working on a codepen project where I had to simplify the code to focus on a specific part that I needed. This section involves using applyMatrix and Matrix4, which are new concepts to me. However, I'm running into some issues where ...

The power of the V8 JavaScript engine: Understanding v8::Arguments and the versatility of function

I have created a Node.js addon that wraps a C++ standard library element std::map<T1,T2>. The goal is to expose this map as a module with two primary functions: Set for adding new key-value pairs and Get for retrieving values by key. I want to create ...

How to retrieve the IP address of a client using Node.js within a setInterval loop

Working on a project utilizing nodejs to fetch client IP Addresses. Within the setinterval function, I have set up the following code: var countdown2 = setInterval(function(){ async function baked(req, res, id){ var getIP = req.headers['x-forward ...

What is the method for implementing an Inset FAB with Material UI in a React project?

Currently, I am working on a project that requires an "Inset Fab" button to be placed between containers. After referencing the Material Design documentation, I discovered that the component is officially named "Inset FAB". While I was able to find some tu ...

Exploring the retrieval of JavaScript array elements from a ListModel within QML

Currently, I have some JavaScript data that consists of a list of objects containing other objects and arrays, which I want to append to a ListModel. This is what the structure looks like (assuming that the data is generated elsewhere and its structure sh ...

What is the purpose of the .Class method in ng.core.component within ES5?

ng.core.Component({ selector:'myapp', template:'<h1>Hello World</h1>' }). Class( { constructor:function() } ); ...

Utilizing the output of a callback function to execute res.render in a NodeJS application

Currently, I am utilizing oracledb for node in order to retrieve data from the database. Once the data is fetched, my goal is to transmit it to the client side using render() in express JS. Below is an example of the code structure: config.js module.expo ...

Customize the element of the root node of a MUI component using the styled()

I am trying to implement the "component" prop with a MUI component (such as ListItem) using the styled() API. However, I am facing an issue where it says that "component" is not a valid prop. Can someone guide me on how to correctly achieve this? I have se ...

Why does everything seem to move in sync with the table's scrolling?

I recently had an issue resolved that fixed most of my concerns: I needed to make three separate edits, and now when I reintroduce the other two edits, they are included within the table section and scroll along with the table instead of being stuck beneat ...

Instructions on how to showcase a standard text within a designated text container

I am currently facing an issue with a textarea or textbox that is dynamically receiving URLs from a database. The textbox is displaying the full URL address, which is not visually appealing. I would like to replace this with some generic text such as "cl ...

Is it possible to define a variable within a JavaScript function and then access it outside of the function?

I have a Node.js application where I need to define a variable inside a function and access its value outside the function as well. Can someone provide guidance on how to achieve this in my code? var readline = require('readline'); var rl = read ...