Publishing a Directory with Flask

Articles Oct 18, 2022 (Oct 18, 2022) Loading...

How I serve python coverage metrics as an html page within replit.com

I need to serve a single directory – coverage metrics from a test runner – as a web page. I'm working in repl.it, and my repl comes with flask, so that's what I'll use to face the web. Serving the page isn't the primary purpose of the work; it's a nice-to-have. The work lives in ./src and ./tests, and repl seems to expect the page to be served while 'running', so this is how I've set up  main.py, in the level above  ./src and ./tests, to serve the page.

## 	Flask server to serve a folder as a webpage
### intended to allow me to run test coverage on the commandline in replit, and to see output as html 

## 	Set up to use the right parts from flask
### 	`send_from_directory` serves a file from a directory
### I could use `render_template`, but I've got no templates. The html etc. is already made
### I could use `send_file` but this is safer as it stops directory traversal
from flask import Flask, send_from_directory

## Make an app, called `app`, to be an instance of flask
app = Flask(  # Create a flask app - minimal?
	__name__,
)

## Routing – so when a browser asks for a resource, it gets something from the app
@app.route('/')  # empty route should serve ./cov_html/index.html
def index():
   return send_from_directory('./cov_html/', 'index.html') 

@app.route('/<path:path>') #Everything else just goes by filename
def sendstuff(path):
	print(path)
	return send_from_directory('./cov_html/', path)

## Boilerplate to start server
if __name__ == "__main__":  # Makes sure this is the main process
	app.run( # Starts the site
		host='0.0.0.0',  # Establishes the host, required for repl to detect the site
		port=8080  # port to serve
	)

Context:

I'm building something for a workshop on mutation testing. It's a workshop, so I want people to work. So I need roughly as many environments as participants. I'm choosing to use replit to allow me to to that.

I need a subject under test, and it needs to be comprehensible. So I'm working in Python. I'm using pytest as my test runner, and I can choose to see coverage with pytest-cov.

The commandline in repl.it is OK for commands, but at least as crap as any commandline for understanding output. `pytest-cov` hjas an optipon to output to HTML – I want to make it as easy as possioble for participants to look at coverage of tests, so I'll take that.

The coverage as HTML is dropped into a directory – and I need a webserver to allow me to browse the information. I can download the directory and look, but it's a pain in the bum. So I need to serve the directory, but I don't need templating.

I want participants to see coverage because mutation testing tells us about what changes to code won't be noticed by existing tests. If the tests don't cover some of the code, then mutation testing will tell us – but coverage will tell us more swiftly, and can help us to understand the output of mutation testing. I'd go as far as saying that if you don't look at coverage first, you're doing mutation testing wrong.

Notes to self

Add more links – to tools, and to workshop

? commnets are as gray as background?

Member reactions

Reactions are loading...

Sign in to leave reactions on posts

Tags

Comments

Sign in or become a Workroom Productions member to read and leave comments.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.