Build an easy prototype site with just Python
You know what’s great?
Making a new model and showing it off to test it more.
You know what isn’t great?
Making a new model and not having a great way to show it off.
It is increasingly easy to pull up some data and make a model (whether shitty or profound). You can train and test (and test some more), but like anything you make, it can be hard to tell how useful it is until its out there working with new data. I have definitely made some models, thought they scored well, and then when tested with new input showed their true colors.
Enter JustPy. This handy Python package offers you the ability to quickly create a webpage in a Python file. It does not eliminate the need for some familiarity with web design languages, but can simplify things significantly, especially for spinning up a test of a model. It also plays very nicely with pandas and sharing data visualizations. I have spent some time creating a basic template that you can use for your own data science projects, but wanted to share an overview of the basics.
Before anything else, let’s get to those two magic coding words. You will first need to just install JustPy, which can be done easily via pip:
pip install justpy
Next, you create a Python file and here is your basic page:
import justpy as jp
wp = jp.WebPage()
p = jp.P()
p.text = 'Hello!'
Let’s go line by line and get an overview. First, we started with importing JustPy and using their preferred abbreviation
jp. Next, we put the entire page in one function, making it clear and easily callable. Inside the function, we start with creating an instance of a webpage with
jp.WebPage(). The important thing to know is we are always going to create that instance of a page, and then add things to it. Just like the webpage, all of our other standard web components exist as classes as well. For this example, we create an instance of paragraph tag,
jp.P() and then setting its text property with a value. With all that in place, we then just return that webpage instance. At the end, we use
jp.justpy to call our function and we have a page!
So something I noticed was helpful to understand was the many options of the tag classes. Let’s look at one div tag:
main = jp.Div(
classes='bg-gray-400 italic', text="My main div", a=wp)
This entails the three main instance variables you will be updating:
classes are the classes you want to add to your tag. An important thing to note with JustPy is that it has Tailwind CSS integrated, a utility-first CSS framework that can be used to easily format and style page elements. Unlike some other CSS frameworks, Tailwind is fairly low-level, so getting the look you are aiming for may take a number of combined classes.
text is fairly obvious, but important to know.
a is the tag your are nesting your tag in. Since you aren’t nesting tags like HTML, you have to tell each tag instance where it needs to be placed. In the example above, I’m just placing this div in our main webpage instance.
I think one of the things that first drew me to testing this out was how seamlessly it could work with input, which can sometimes feel like a chore when connecting Python and HTML. There is the ability to use forms, but for a simple text input example, you can have something like this:
in = jp.Input(placeholder='Please type here')
Boom, input! The value entered will be stored in the class instance, in this scenario it would be in
in.value. You can see live what is input with an asynchronous function or store and submit for processing.
Okay, so we have some understanding of the basics, let’s get to an example! Let’s say you’re tasked with creating a spam detector for corporate email accounts. You have a nice labeled set of emails, and built a pretty straight forward logistic regression model that you feel pretty good about. And then it happens, someone says “Can I try it out?”. Instead of panicking, pickle that model, and roll it into a new Python file with your webpage!
To help ease the design, I found a site with Tailwind templates and used that as a base. I created two main functions, one to process input and one for the webpage. So in just one Python file, viola:
Looks pretty good! On top of that, it doesn’t require an extensive directory of files or dependencies. My entire page is built with one Python file, and has one other where I built my model. I set it up to just automatically process input as it comes, but you can see it does alright with looking for spam.
And the bad:
You can jump off from my repo here and create your own easy JustPy model webpage to test our your own data science projects. Enjoy!