Compiling Coffeescript with Flask in Production - python

I have a Flask app that I'm running in production. Right now it has a big ugly js file that I'd like to break out and rewrite in something like Coffeescript. I was considering something like Flask-Cake to simplify the CoffeeScript compilation. However, I don't know how something like that would work for production. I should probably have a script that compiles the coffeescript files before deploying, right? I've never worked on a system with this particular layout -- uncompiled server-side but compiled client-side. What's the standard procedure here?

You are probably looking for Flask-Assets.
Example from the website:
from flask import Flask
from flask.ext.assets import Environment, Bundle
app = Flask(__name__)
assets = Environment(app)
js = Bundle('jquery.js', 'base.js', 'widgets.js',
filters='jsmin', output='gen/packed.js')
assets.register('js_all', js)
This would automatically concatenate jquery.js, base.js and widgets.js in your static folder, pipe them through jsmin and save the result in static/gen/packed.js.
This compilation is by default always happening when one of the source files changes. Watching the files in production is kinda expensive in production (and would require a coffeescript compiler to be installed on the server!), so there is a configuration value to disable the monitoring.
Another plugin that is more lightweight, but in my experience also less powerful is Flask-Makestatic.

Take a look at DukPy! It's a simple javascript interpreter for Python and can compile CoffeeScript, TypeScript, BabelJS and JSX. Usage is very simple:
import dukpy
dukpy.coffee_compile("CoffeeScript goes here!")
DukPy is the successor to the Python-CoffeeScript package witch is no longer maintained.

Related

Building an EXE with a Flask server included

I'm attempting to build an application that runs locally on a user's computer. It takes inputs and displays information, so I thought a nice way to make it work would be to have a Flask server that handles web forms, etc, to gather user input, and to display database contents in tables.
The service works when I run the Flask development server, and everything is fine and dandy, but I want to compile the entire project into a single executable file.
My questions are as follows:
Is it possible for me to have the flask server automatically start up and open the portal page when I double-click the EXE
Assuming this is a feasible concept, how would I go about compiling the exe? A brief look through the pyinstaller documentation does not tell me how I would compile a folder (which in this case would be more of a Python package), which would be a necessary pre-requisite considering that Flask has static and template folders containing my HTML, CSS, and JS files, not to mention the fact that my file tree is built in a modular manner.
Am I approaching this the wrong way? Would it be better to build a "launcher" application that runs Flask as a Windows service (vis a vis this question) instead of attempting to have an all-in-one EXE?
For reference, my file tree looks something like this:
Application
|_static
|_templates
|_\templates\errors
|_\templates\forms
|_errors
|_database
|_\database\connections
|_\database\iohandlers
|_"__init__.py"
|_"app.py"
While doing research on this, I came across this answer but as my situation doesn't mesh with the model that the OP had laid out, I thought it would be pertinent and useful to ask another question.

Serving static files with App Engine Python 37 without any Python script

With App Engine Python27(standard env), it was possible to create a webapp that only serves static resources from the app.yaml. Without writing any Python script.
With Python37 (standard env), I see that it's still possible to have the static_dir in the handlers section of app.yaml but I'm wondering if this will work the same way. Cause I don't define any entrypoint, I don't set a webserver. I have only the app.yaml at the moment.
And the first that blocks me at the moment: if doing so, how do I locally run this app ? (no Python script so no python main.py and dev_appserver.py does not work for Python37).
Thanks for your answers
Yes, the static_dir and static_file configurations work the same way, their sections in the app.yaml reference docs for 2.7 and 3.7 are identical except for the references to the 2.7-specific application_readable option.
Since serving the static content on GAE is identical (the static content is uploaded and served separately from the application code - equivalent, if you want, to serving from a CDN) it doesn't really matter if you're using the 2.7 runtime or the 3.7 one - you're not actually executing any python code, right?
So one option would be to just use 2.7 instead (adding a minimal app skeleton to keep the runtime happy, say by just returning 404 or redirecting to one of the static pages - you can get it from the Quickstart). You can then continue to use the 2.7 development server for local execution.
Another option would be to use 2.7 (as in option 1) just for local development, but switch back to 3.7 for deployment (i.e. update the app.yaml file, drop the skeleton app code or update it for 3.7). A bit more tedious and brittle, but it can be done.
A 3rd option would be to try the updated development server which has some limited support for 3.7, see Python 3.7 Local Development Server Options for new app engine apps. Serving static content might be included in that limited support. You'd need to meet its specific requirements. You'd also need a 3.7-compatible skeleton app, you can get one from the 3.7 Quickstart.
Finally, you could also use some other tool locally during development, if you have one (same advice as for running an actual app locally). It could simply be your browser for static-only content :) Again, the goal is to just develop your static content, GAE will serve it the same way. You won't need any skeleton app in this case.
Side note: I saw this in the Node.js standard env app.yaml reference, not sure if it's applicable to python 3.7 as well, just be aware of it:
In order to use static handlers, at least one of your handlers must
contain the line script: auto to deploy successfully.

Which lightweight python web framework has applications and hot-deployed code

I am used to PHP having applications. For example,
c:\xampp\htdocs\app1
c:\xampp\htdocs\app2
can be accessed as
localhost://app1/page.php
localhost://app2/page.php
Things to be noticed:
a directory placed inside the www-root directory maps directly with the URL
when a file/directory is added/removed/changed, the worker processes seamlessly reflect that change (new files are hot-deployed).
I am on the lookout for a mature python web framework. Its for a web API which will be deployed for multiple clients, and each copy will diverge on customization. And our workflow has frequent interaction/revision cycles between us and our clients. Hence the "drag and drop" deployment is a must.
Which python framework enables this? I prefer a lightweight solution (which doesnt impose MVC, ORMs etc)
Related
How to build Twisted servers which are able to do hot code swap in Python?
Python web hosting: Why are server restarts necessary?
fastcgi, cherrypy, and python
No mature python framework that I'm aware of allows you to map urls to python modules, and frankly, for good reason. You can do this with CGI, but it's definitely not the recommended way to deploy python apps. Setting that requirement aside, flask and bottle are both lightweight micro web-frameworks with similar approaches, both allow you to reload automatically when changes are detected (this is only wise during development).
There is no web framework in Python that I know of that lets you do that out of the box, but if you need it it's not to hard to add with a bit of convention over configuration.
Simply pick your web framework of choice in Python and then write a wrapper to the main application that walks a directory or set of directory and auto-registers routes from the modules inside of them. Have your modules do the same thing in their __init__.py files to the other files located with them. Then just set up your WSGI code to autoreload when the WSGI script is updated and your deployment during development simply becomes a two step process - add file then touch dev_app.wsgi. You could even add a real deployment option to this wrapper that walks a set up dev environment like this and generates hard-coded URL-to-function mappings for deployment.
However, all of this work isn't really necessary. Python is not PHP and the way you develop in one doesn't necessarily translate to the other well. If the client wants variable routes, use dynamic routes and give them (or you) an admin interface to control the mapping of content to URL. Use flat files, SQLite, a NoSQL datastore, or the ether to store these mappings and the content. Use a template engine like Jinja2, Mako, Cheetah or Genshi to maintain your general layout. Wrap this all up with an object oriented structure to make extending it easy (or use a functional paradigm if that comes more naturally to you). Or, drop the whole dynamic in production portion and generate flat HTML files a la Jekyll.
CherryPy is a mature web framework that redeploys automatically when changes are detected. The file structure - URL isn't there, but it is a lightweight framework that doesn't impose ORM, MVC, or even a templating engine.
If you are used to PHP, you might want to take a look at the Apache modules mod_python or mod_wsgi (and WSGI in general if you do web development -- which is the Pythonic way).
With those two modules, the Python interpreter gets started every time a request comes in (similar to PHP). Needless to say, this slows things down but you'll always get the result based on your newest code. Depending on your expected traffic numbers, this might or might not be okay for you.
BUT: If you decide to write your own framework, you most probably do not want to write a system that supports "hot-deploying". Even though the reload() command is built-in, it takes more than just that and will get you into a world full of pain.

App Engine Development Server Does Not Reload Code When Changed

I'm working on a python app that will run on top of Google App Engine. I setup my app up with the following directory structure:
approot/
app.yaml
index.yaml
myapp.py
controllers/
some_controller.py
some_controller1.py
models/
views/
...etc...
My problem is that the development server will not always automatically reload my code when I make changes even though Google's documentation says it will.
The only time it does reload my code is when the change I make is in the top level directory of my app. Anything in a subdirectory (e.g. controllers) is ignored. I have to stop and start the server every time a change is made.
I find this really impedes my progress in development, especially since there is no restart button, you actually have to hit stop and then start.
Is there a remedy for this or am I just doing it wrong? I really like having a well organized project and would rather not dump all my files in the top level directory.
The reload mechanism is likely tied to the default import mechanism and builtin __import__ function. If you (or your framework) load your modules in some other, clever way, the reloader might not notice. A possible workaround is to explicitly import key modules in your myapp.py module.
double check that you have installed pyobjc
the dev server will complain like this:
UserWarning: Detecting source code changes is not supported because your Python version does not include PyObjC (http://pyobjc.sourceforge.net/). Please install PyObjC or, if that is not practical, file a bug at http://code.google.com/p/appengine-devappserver2-experiment/issues/list.

Installing Sphinx on App Engine - possible?

Following up on my last year's question on documentation, I now want to get started and try out Python-based Sphinx for putting together the developer documentation for a PHP CMS I've been working on.
Instead of setting up Python locally on my workstation, I would like to run it on a publicly accessible web server from the start. All the web hosting packages I have access to run on the LAMP stack, and I'm reluctant to buy Python-based hosting. I am very interested in the Google App Engine, the free quotas they provide will do for me a hundred times over, and even if not, their pricing looks very reasonable.
Now I have zero knowledge of Python - getting Sphinx to work would be my first contact with it - and very little time. As far as I understand, the platform and python libraries the App Engine provides are very compatible to a standard Python library but not identical.
So my question is:
Can Sphinx run on App Engine at all?
Is installing Sphinx on the App Engine as straightforward as if I would install it on top of a normal Python installation? Or will the App Engine's environment require tweaking of the source code that I can't perform in reasonable time with my current level of Python?
Should I be installing Sphinx on a local server and a "normal" Python stack instead first?
Does anybody know any helpful How-to's, tutorials or other resources for this?
You do not need to install Sphinx on GAE at all.
You use Sphinx to create a directory of static HTML, CSS and JS. When this step is finished, you simply upload the output from Sphinx -- in it's entirety.
The output from Sphinx (HTML, CSS and JS) is simply served from one place. You upload the documentation from where you created it to GAE as static files and serve them. Done.
There's no "install on a web server" aspect to Sphinx at all. Sphinx is not a web application; it does not run on your web server. You run it in development briefly to publish the documentation to HTML, LaTeX or whatever. Once you've created the static HTML files, you no longer need any part of Sphinx anywhere.
Can Sphinx run on App Engine at all?
I suppose it can, but you never need to.
Is installing Sphinx on the App Engine
as straightforward as...
It doesn't matter, because you don't install it there. You install it on your workstation and use it on your workstation.
Should I be installing Sphinx on a
local server and a "normal" Python
stack instead first?
Don't install it on a server. Install it on your workstation.
Does anybody know any helpful
How-to's, tutorials or other resources
for this?
If by "this" you mean "installing on a web server", then then answer is "no". One does not install it on a web server. So there are no how-to's, tutorials or resources for "installing on a web server".
If by "this" you mean "creating documentation with Sphinx", then the answer is "what's wrong with https://www.sphinx-doc.org? What do they lack?
I would like Sphinx's "engine" that
turns the input (consisting of reST
files) into HTML/CSS/JS to be
accessible from anywhere to make me
(and possible other contributors)
independent from a specific
workstation.
Sphinx is like a compiler. Everyone has their own copy on their workstation. They download the document source, make changes, commit the source changes, and upload the resulting document.
serve the generated documentation from
the same place.
Correct. After you download the source, make changes, regenerate the document and commit the changes, you upload the resulting document so it will be served from one -- and only one -- place.
Sphinx is a compiler. It is not a "web engine". It's a simple compiler that simply compiles your documentation into HTML so it can be served.
Interesting project! The main issue you're going to run into is that of filesystem access: The filesystem on App Engine is read-only, and based on looking at the source, Sphinx is fairly hard-coded to use the filesystem for output. It also expects to read the configuration file and input files from the local filesystem, which would make building docs for anything other than projects bundled with the app tricky.
It is possible to work around this, but it would require writing a simple virtual filesystem that uses the App Engine datastore, and using monkeypatching to make it work with the regular Python file interface. That's rather advanced for a "new to Python" project!
One other thing you might want to consider if you were pondering allowing users to upload projects to be documented: The Sphinx configuration file is in Python, so executing it could be dangerous - a user could do nasty things to your app with a malicious configuration file!
The difference between deployment locally via dev_appserver and remotely via appcfg has been - in my experience - limited to which of the two commands I execute. That said, I've no experience with Sphinx.
Sphinx compiles documentation, running it on a webserver makes as much sense as running gcc on a webserver.

Categories

Resources