How to define one hops component per python file?

Hi, while getting familiar with hops with the wonderful video

I was wondering how to define one hops component per file ?

In the following code, I define 2 components in the same python code. I was unable to split the code in two files. I apologize in advance if the answer is obvious but I am a python beginner.

import rhino3dm
from flask import Flask
import ghhops_server as hs

# register hops as middleware
pointatServer = Flask(__name__)
hops = hs.Hops(pointatServer)

@hops.component(
    "/pointatopposite",
    name="PointAtOpposite",
    description="Get Opposite Point Along the Curve",
    icon="pointat.png",
    inputs=[
        hs.HopsCurve("Curve", "C", "Curve to evaluate"),
        hs.HopsNumber("t", "t", "Parameter on curve", default=2.0),
    ],
    outputs=[hs.HopsPoint("P", "P", "Opposite point on curve at t")],
)
def pointatOpposite(curve: rhino3dm.Curve, t):
    newT = curve.Domain.T1 - t
    return curve.PointAt(newT)

@hops.component(
    "/pointat",
    name="PointAt",
    description="Get Point Along the Curve",
    icon="pointat.png",
    inputs=[
        hs.HopsCurve("Curve", "C", "Curve to evaluate"),
        hs.HopsNumber("t", "t", "Parameter on curve", default=2.0),
    ],
    outputs=[hs.HopsPoint("P", "P", "Point on curve at t")],
)
def pointat(curve: rhino3dm.Curve, t):
    return curve.PointAt(t)

if __name__ == "__main__":
    pointatServer.run(debug=True)

hi, I finally was able to define a project with separate python file for each component. It was not straightforward. I used the vscode tutorial

The result can be seen in the project below (have been test on macos only)

Hey !

I’d be interested to know why you’d want something like this…
I prefer to have all my Hops functions in a single file and all the utility functions in another (or few others), but I may be missing something :slight_smile:

Hi Antoine,

I want something like this to have a better configuration management of GH definitions.

I have GH definitions that grows to >500 components. This is a proving difficult to manage. Last week, I made a modification in a very big .gh file and the output fell apart with no apparent reason.

This is the main reason I am very exited by Hops and I want to switch to GH/Hops ASAP. I will be able to break down a GH definition into as many files I want and each configuration change will be human readable as a python code change. I will loose the fun of drawing the components / links but I have to find the sweet spot between fun and configuration management.

In order to do that, I need to spread my python/hops code into as many files I want, hence this topic.

Hopes the answer is clear.

Apologize if my question wasn’t clear…
I wanted to ask: why would you want to have one Hops function per python files?
It’s a true question, I’m not saying it’s wrong, just wondering why it could be better :slight_smile:
I guess it’s easier to maintain your code, at the cost of duplicating the import of libraries on every files?

Here’s an example where I have all my Hops functions in one file, and I can still call any of them in any Gh definitions…

1 Like

Hi Antoine,

you’are right, my point is to avoid too large python files. I translated this into one hops component per python file (an intellectual shortcut for sure).

I had a look at your scripts in your hops repo (nice project BTW). Your biggest script is hops_plots.py which 582 SLOCs (Source Line of Code) which is still manageable.

Best practices suggest that the max SLOCs per file should be between 300 & 2000. It is a matter of taste probably. In my case, I tend to stay below 500 SLOCs per file. My idea for the port of my very complex GH file to hops is to break it down into python file of no more than 500 SLOCs. I surmise I will one hops component per file but I might be wrong on that.

Hope it is clearer this time

It is now, yes.
Thanks for the details ! :slight_smile:

I’ll remember your post in case I need to split files for different groups of Hops functions !