1

html is just text, often very repetitive. I have a simple homework webpage. I'm trying to automate the webpage production.

I have Python routines for producing html (I made them, so they are primitive, but they all work):

makeCheckboxes.py  
makeDropdownboxes.py  
makehtmlTable.py  
makeRadiobuttons.py  
makeTextboxes.py  
makeThankyouPHP.py  

and lastly:

makeWebpage.py

They all just output a text file.

Rather than lump all these in one very big, long file (I lose the plot easily), I'd like to call the one or ones I want from makeWebpage.py and run it, then knot the sections together into 1 text file.

They are all in /home/pedro/textTohtml/ I run them in a bash terminal.

I don't need all the routines each week.
All I need to know is, how many sections I want and what's in it.

For example, let's say next week: Section 1 is radio buttons, Section 2 is Textboxes (fill in the gaps exercise)

Can I call the 2 routines from makeWebpage.py without actually defining them within as functions?

The functions themselves produce a text file which I can then open and integrate into the webpage template.

EDIT: Thanks for the answers. What I need is to import the whole file, each of which will then have its own inner functions. If I do this:

import file as fl

Will it then run fl? Or is it better to run subprocess?

Pedroski
  • 433
  • 1
  • 7
  • 16
  • Note, python has functions, generally the terminology routine is not used What you have here are *modules.* Depending upon how you've organized them, i.e. by populating them with re-usavle functions, you can use code written in modules wherever you want. – juanpa.arrivillaga Apr 17 '19 at 00:25
  • If you are going to be reusing code, you will need to define some functions, even if you keep those functions is a separate file. Technically, you can run each file's code using `subprocess` but what you really should do is wrap the code in `def`s and use `import`. – ktdrv Apr 17 '19 at 00:26

2 Answers2

1

How does this help you:

Call a function from another file in Python

You just need to use

from file import function

To import the functions at the start of your makeWebpage.py file. Then makeWebpage.py can call any of the functions any time it wants.

Moffen
  • 1,817
  • 1
  • 14
  • 34
  • 1
    Woohoo! I managed to import makeRBsV1 and it worked like a dream! Thanks for the link! Apparently I don't need to write .py – Pedroski Apr 17 '19 at 01:22
  • A problem: I do 'import makeRBsInlineV1 as fl' and my program runs perfectly. BUT it starts straight away. I can't assign it to a string variable. I need to assign the output to a string variable. I modified the python, put return htmlString at the end. How can I assign the output to say myString?? This just gives an error:>>> string = import makeRBsInlineV1 as fl SyntaxError: invalid syntax – Pedroski Apr 17 '19 at 03:20
  • try: htmlValue = yourMethodName() – Moffen Apr 17 '19 at 03:26
  • 1
    Yesterday I had trouble with this, but I am a morning person. This morning at 5am I figured it out very quickly! Got all my modules imported into makeWebpage.py! Thanks for the tips! – Pedroski Apr 18 '19 at 00:27
1

If you only need a function:

from FILE import FUNCTION
FUNCTION(*args,**kwargs)

If you want to run/execute the python file (everything):

import os
os.system("FILE")

[NOTE] The file must be a string and contain the extension, eg "some_file.py" whereas in import statements only the file name is specified in plaintext.

In both cases both files must be in the same directory unless a path is specified ("c:\Users\MyProfile\PythonFiles\python.py" or "/Users/MyProfile/PythonFiles\python.py")

EDIT: If you are importing an entire file (STRICTLY LIKE THIS!) from FILE import * you can name variables or functions with an underscore before it to prevent it from being imported (more info on PEP 8 https://www.python.org/dev/peps/pep-0008/#id36)

Eric Jin
  • 3,836
  • 4
  • 19
  • 45