Python and Javascript in same project?

Is it possible to run both Python and Javascript code from the same project, e.g. run a Node and Flask server on different ports maybe?

That’s a really cool question – can you give some more detail about what you’re trying to do, or how the scripts would interact with each other?

Hey @Suds-p, like @househaunt I’d love to hear more about what you’re trying to do.

Glitch project’s only allow external traffic on a single port so you won’t be able to expose both a Node server and a Flask server to the internet - only one of those can run on the port Glitch is listening for (typically 3000, but it can change and - in Node - process.env.PORT will always have the right value provided you don’t overwrite that information).

If you wanted to have Flask running some sort of internal API, for example, it could run on some other port that would be available inside the project for Node to access, or vice versa. You might also be able to do something like what’s discussed in What's the port range users have at Glitch? to expose both servers by proxying requests through Node to Flask.

Hope this helps!

2 Likes

Hi @cori and @househaunt! Thanks for the information, I didn’t know projects only ever had one port exposed. In that case I probably couldn’t do what I wanted? But here’s what I’m trying to do anyway:

So my Javascript side is my frontend, where people can log in with Google authentication. But my Python side (backend) also needs Google authentication for it to make Google API calls. I’m using the pydrive library for the API calls which has 2 auth methods, either opening a new browser tab with the sign-in flow (which I use) or entering information into the command line, both which require manual user interaction.

What I wanted to do is, if there weren’t any cached credentials already, or the user signed out, it can run the pydrive authentication in a new browser tab, get the information it needs, then the JS side can sign in as the new user and run as normal.

So far, I tried telling Flask to run on a different port:

...
@app.route('/pythonmain')
def main():
  gauth = GoogleAuth()
  gauth.LocalWebServerAuth()
  return "i'm from python"
...
if __name__ == '__main__':
  app.run(port=3001)

And also had JS set up to invoke the Python code with an AJAX call:

function login() {
  $.ajax({
    type: "GET",
    url: "http://127.0.0.1:3001/pythonmain"
  }).done(e => console.log("this worked???"));

but the project server kept hanging when it was trying to start up because it couldn’t actually run “node server.js && python3 server.py” for reasons you mentioned.

@Suds-p Could you link to your project? I think I see what you mean but want to make sure I’m understanding.

My first thought (without having seen the code) is that you might not need that node server – if the Javascript is just the frontend (it’s only making that ajax call) would it be possible to serve the Javascript from flask, or am I missing something?

It sounds like a really cool project!

So, one other complexity here is that you may need to “manually” install your python requirements - Glitch will automatically make requirements.txt work in the absence of package.json, but if it’s there then it won’t do any boilerplate python setup. You could work around that by using a shell script that calls pip3 install as well as python3 server.py in your package.json’s start script.

2 Likes

@jmsoper @cori Hi! Sorry I took so long to respond. I haven’t thought of serving the Javascript from Flask since I haven’t done anything like that before. :sweat_smile:

So far, I’ve set up an alternate solution for my app to avoid needing to use any local server authentication from the Python library, so I can simply have two separate projects running both the Python/JS sides that communicate with each other now. I think my main issue was not understanding how to use Google Auth as I needed in the first place.

If I ever actually need to combine both languages in a project, however, I’ll be sure to check out the resources y’all linked! And thanks so much for both of y’all’s help. :slight_smile:

2 Likes