0

I just started JavaScript. I wrote some code with javascript using the library: Three.js, now I wanted to do the Backend with Python. Therefore i started using Flask. So I have my index.html in the templates directory, inside my index.html i call my script.js like this:

<script type="module" src="/static/script.js"></script>

In my script.js I import three.js in the beginning like this:

import "/three.js"

The Three.js file is in the same static folder. But somehow the import doesnt work when i use Flask.

0

2 Answers 2

1

First of all, the 'import' statement hasn't yet widely supported by browsers, so if you really want to use it, you have to use a module bundler e.g. webpack, broswerify, to wrap all files into one big js file. However, if you are not familiar with those tools, to be honest, you have to spend many time to learn how to use them.

So I recommend you to keep things simple. For example, you can first make a copy of the library's source code and save it to your project's static folder, so that it can be served publicly. Second, you can create a HTML file, with following template:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100% }
        </style>
    </head>
    <body>
        <script src="/static/three.js"></script>
        <script src="/static/script.js">
    </body>
</html>

P.S. this example is from Three.js offical document, with a little modification.

Since all scripts in a HTML share a same global environment, after the first <script> tag is loaded, the code in the next <script> tag can access three.js's global variable that is loaded before!

Sign up to request clarification or add additional context in comments.

6 Comments

I've deleted my comment on the question since this is a great answer. I'd add, since this might not be obvious to OP but that we often take for granted: the UI code is completely disconnected from Flask and has basically nothing to do with it. You write JS and HTML and send it naively to the browser, who then executes it. I intuit that OP might be conflating ES6 import {} from "module" with Python's import <module>...
Yeah, I ghest @Oroshimaru is not familiar with writing website codes. No judgement.
Certainly not, I'm merely augmenting your good answer with some more information that might be glossed over as assumed. :) You would even edit your answer to add that, and flag my comment as "No longer needed" if you like.
@Stephen.W Thank you for your answer! Yes im new to writing website codes. Just one last Question, in my Script I have imports like import {STLLoader} from "/STLLoader.js". But when I import the STLLoader in the html file like you did my code wont work anymore. Do you know how to fix that? I tried things like changing my code from new STLloader()to new STLLoader.STLloader() but that didnt work either.
And I also just recognized that the STLLoader.js is also importing from three.module.js.. So that wont work either ?
|
0

flask is mirco framework, so you need to follow the documentation instruction on using static files.

have a look on this post

where to keep js files in Flask applications?

1 Comment

I have my .js files in a stativ folder and my html files in the template folder. the porblem is im trying to use a js script inside another js script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.