Transform Your Python Script into a Web App with CalcTree: A Step-by-Step Guide

Kamal samaila
Python in Plain English
6 min readMay 6, 2024

--

Engineers often rely on Python scripts to solve or verify the result of complex computational problems. However, sharing and collaborating on these scripts can be challenging, and they may not always be user-friendly because not everyone is good at coding. CalcTree offers a solution enabling you to convert your Python scripts into web applications, making them more accessible and shareable. This article will show you how to transform your Python script into a web app using CalcTree.

What is CalcTree

CalcTree, is a calculation management platform. You can build hosted, shareable web apps (complete with an API and a web publishing module) with tools like Python and Spreadsheets.

Structuring Your Python Script for a Web App

Before converting your Python script into a web app, it’s essential to structure the problem into inputs and outputs. This will help you understand how your app will receive input from the user, transfer the input to Python code for computation, and finally transfer the calculation output back to the page for the user.

Here’s an example of how you might structure the input and output for a Python script that designs a baseplate according to EC3.

Now, let’s explore the three basic steps to convert your Python script into a web app using CalcTree.

Step 1: Add Necessary Input Parameters to the Page Content

First, add the necessary input parameters to the page content. This will allow users to input the required data for your Python script to compute. Follow these steps:

  1. Type / on the page to receive the command menu
  2. Click on the Parameter field or press Enter to insert a parameter on the page. Change the default Untitled title of the parameter to an appropriate symbol.

Here’s an example of what the input parameters might look like for the base plate design:

Step 2: Add Your Python Code

Next, add a Python source to your page. Follow these steps:

  1. Go to the Integrations panel on the right sidebar and click the “+ Add” button.
  2. Click the “Code” button to add your Python source.
  1. Write or paste your Python code inside the code editor, in the window at the bottom of your page.

Here’s an example of what the code might look like for the base plate design:

Acol=_page_Area 
Pcol = _page_Perimeter
# Partial factor of resistance of cross-sections whatever the class is as per EN 1993-1-1.
Ym0 = 1.0
# Compute ultimate load (Ned) -> on page# Compute foundation bearing strength which is typically concrete#βj is the foundation joint material coefficient, typically taken as 0.67 as per clause 6.2.5(7) in EN 1993-1-8.
beta_j=0.67
#α is a coefficient of diffusion of the vertical load being applied to the foundation. Conservatively this can be taken as 1.5
alpha= 1.5
# αcc is the coefficient that allows for long term effects on the compressive strength of concrete vs applied actions. Taken as 0.85 in the U.K National Annex -> on page# γc is the partial factor of safety of concrete. Taken as 1.5 in the U.K National Annex -> on page
fjd = beta_j*alpha* (_page_alpha_cc*_page_fck )/_page_gamma_c
# Compute area of baseplate required
Areq = (_page_Ned *1000)/ fjd
def calculate_c(Pcol, Acol, Areq):
# """
# This function calculates the value of c for the given equation:
# Areq = 4 * c^2 + P_col * c + A_col
# Args:
# Perimeter_of_section: Perimeter of the column section (mm)
# Area_of_section: Area of the column section (mm²)
# Areq: Required area of the baseplate (mm²)
# Returns:
# The value of c (mm)
# """
a = 4
b = Pcol
c = Acol-Areq # Assuming Areq is already calculated
discriminant = b**2 - 4 * a * c
c1 = (-b + (discriminant)**0.5) / (2 * a)
c2 = (-b - (discriminant)**0.5) / (2 * a)
return max(c1, c2)
c = calculate_c(Pcol,Acol,Areq)# get the length and width of baseplate
Plate_L=_page_h+(2*c)
Plate_W=_page_b+(2*c) # Check that there is no overlap between the flanges
if c < (_page_h - (2 * _page_tf))/ 2:
check = "OK, there is no overlap between T-stubs."
else:
check = "Overlap between T-stubs, calculator not valid. Decrease Ned or increase column size."
Aeff = 4 *( c**2) + (Pcol * c) + Acol# Compute the thickness of baseplate (tp)
tp = c * (3 * fjd* Ym0 / _page_fy)**0.50

👉 Note all the input page parameters are accessed by Python using “page” prefix, e.g. _page_b , _page_h, _page_tf. You can just type the name of your page parameter in the code editor, and a suggestion box will identify and allow you to select your parameters on the page.

Step 3: Add Calculated Python Parameters to Page Content

Your output parameters will appear on the right-hand side of your screen, in the page parameter list under Code source. To add these to your page, you can simply click and drag them to the page.

This will display the results of your Python code on the page for the user to see.

Here’s an example of how you might arrange the output parameters for the base plate design:

👉 Note each output parameter is a block on the page, which can be grabbed and moved around the page, just like other page elements.

The app is ready to be published, see the live base plate designer in the link below:

🔗 Steel Base Plate Designer to EC3

Conclusion

Engineers can convert their Python scripts into user-friendly web applications. By following the three basic steps outlined in this article, you can transform your Python script into a web app, making it easier to share and collaborate with your colleagues.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--

I believe that knowledge and intelligence is what humanity will always use to solve emerging problems facing our dear planet