With notebooks that is not possible.
You can write your code in scala/java and build a jar, which you then run with spark-submit.
(example)
Or use python and deploy a wheel.
(example)
This can become quite complex when you have dependencies.
Also: a jar etc can be decompiled rendering your usecase useless.