Clojure and Python Flask

Does somebody has experience serving ClojureScript (Re-Frame) through Python Flask? Or even better integrating a Clojure app within an existing Flask app ? I need to add a new module to an existing Flask app and I’d like at least the frontend to be ClojureScript. What are the possibilities?

Kind regards,
Pascal

1 Like

The important thing to understand here is that the primary thing that your Flask service really does is allow end users to download and display formats that are supported in a browser. ClojureScript is not supported in any browser. In fact, browsers typically only really provide support for three things: HTML, JavaScript, and CSS.

When you make a frontend application you might write the code in ClojureScript, maybe using reagent components returning Hiccup, and perhaps even some Clojure DSL for writing CSS. It doesn’t really matter. At some point you will need to compile it to the 3 formats supported by browsers.

Even developers writing frontend applications in JavaScript typically need to compile their code. They write it in the latest version of JavaScript and then use babel or some other tool to compile it to version of JavaScript that is fully supported by all major browsers. If they use React, they don’t write HTML either. Instead they usually write JSX which is an HTML-looking DSL similar to the Hiccup used in reagent/Re-frame.

So no matter what you do, there is always bound to be a compile step where you compile whatever you have written in whatever combination of languages to HTML, JS, and CSS. Your Flask app simply returns that HTML, JS, and CSS. The user’s browser doesn’t care what it was before it became HTML, JS, and CSS.

However, if you’re making a ClojureScript frontend application you should consider using transit to send data back and forth between your backend and frontend application. In your case you probably want these

Transit works best if you have Clojure on the backend, since then you can almost treat your backend and frontend code as one single Clojure application. I think it will work pretty well for Python + CLJS too, though.

Hi Simon. Thanks for clearing things up. Is there away to mix Flask and Clojure (for the backend). Eg. some route within an application would go t the Clojure backend. The reason I’m asking is that I’d like to introduce Clojure and ClojureScript in a Python/Flask shop. In a gently manner by writing some new modules in Clojure/ClojureScript while the older stuff remains Python/Flask/Javascript. For frontend, as you explained, there is no problem as ClojureScript compiles to javascript. So Flask/Python for backend and ClojureScript for Frontend should work without problems. But I wonder if I could also integrate Clojure for backend ( while still having to total application in Flask)
Pascal

Backend I doubt there is a way, unless you can get flask running inside Jython maybe, or Graal Python, and that somehow there’s an interop with Java in there (and thus Clojure).

You could also possibly proxy some routes from Flask to a Clojure backend app, but that would be a bit hairy.

Something else you could do is front your flask app with some reverse proxy that splits some of the request to some Clojure backend, maybe even you can use AWS Gateway to front your app with it and have routes in it that go to flask/Clojure

1 Like

What didibus said.

The typical way to have multiple backends running - e.g. if you are in the process of replacing one with another - is to proxy requests. So you simply forward certain requests through your Python service to the Clojure one and return whatever the Clojure one is returning in the Python one, trying to keep those Flask endpoints as simple as possible.

1 Like

Hi simon and didibus. Thx for lighting my mind…

So I think I’ll just use ClojureScript for the frontend and stay with python/Flask in the backend for this case.

You could try interop with python via libpython-clj, but it’s an atypical use case. Another option would be writing a microservice in clojure and tying it in that way.

1 Like

That’s probably a good way to get started in “gaining minds” as it were. If they get used to the benefits of cljs for the front end, they might be willing to consider some backend components in clj…
I work in a .NET shop, and I’m doing most of my stuff in Clojure, using the approach described by @simongray, and proxying calls to my own clj backend functionality from our current .NET API. Eventually it will be standalone, but this is a good migration path, I think.