Python Forum
Make bash call my own Python modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make bash call my own Python modules
#1
I have a little homework webpage. I'm trying to automate the production of the webpage each week.

I know very little about this. Wednesday I asked here about importing Python functions that I made. I think I have grasped the concept. I've tested everything in Idle. All the modules work.

In my Python shell, (I use Idle in Ubuntu), everything does what I want.

I first append the paths:


for i in range(0, len(pyPaths)):
sys.path.append(pyPaths[i])
then import the modules:


from makeRBsInlineV1 import makeHTMLrbsNums
from makeCheckboxesInlineV1 import makeHTMLCBs
from makeDropdownboxesInlineV1 import makeDropdownboxes
from createhtmlTableInlineV1 import makeHTMLtable
from makeRBsInlineV2 import makeHTML_RBs
from readLinesTextboxesInlineV1 import readLinesmakeTBs
from makeThankyouPHPInlineV1 import makeThankyouPHP
All these modules return a text string which is a mixture of html tags + my text. It displays nicely in Firefox.

In Idle I just write, for example:

myString = readLinesTextboxesInlineV3()
it asks me a few questions and off it goes. Afterwards, myString is ready to be put in the webpage text string.

However, in bash, I cannot write


myString = readLinesTextboxesInlineV3()
in bash, I cannot write


myString = input('Enter the name of the module you want. ')
and then enter,


readLinesTextboxesInlineV3()
because then myString is just the input text, not the function.

Each week things are different, I may need to run 2 or more of the modules, add the result strings, then write them in the webpage text string in the correct insertion point.

I run this in a


while True:
loop, so I can add strings from various modules.

For any given week, I don't know which module I want to use, so I make them all available.

I have a list of all the modules:

pyFiles = ['makeCheckboxesInlineV1()', 'dropdownboxesInlineV1()', 'createhtmlTableInlineV1()', 'makeRBsInlineV2()', 'readLinesTextboxesInlineV3()', 'makeThankyouPHPInlineV1()']
How do I assign myString to any 1 of the modules above when I run makeWebpage.py in bash?
Reply
#2
Just put the functions into pyFiles instead of strings:

pyFiles = [makeCheckboxesInlineV1, dropdownboxesInlineV1, createhtmlTableInlineV1, makeRBsInlineV2, readLinesTextboxesInlineV3, makeThankyouPHPInlineV1]

# iterate over the list
for function in pyFiles:
    print(function)
    function()
I won't show how you can evaluate strings, because it's in 99.99% of the cases wrong.
From bash world you know this as command substitution.

Your names are looking, as the are coming from Java-World.
Read more about naming conventions in Python.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I can print the names of the functions with your python, but there is no way to make them run like that in bash!

I'm not coming from Java, I just don't know how to do it.

I'm pretty good at using openpyxl, because I need to.

import openpyxl

do all kinds of stuff.

How do I do that with my own modules???

I put this after I import the modules, but it does not start:

Quote:pedro@pedro-newssd:~/myHWpageSummer2019/textTohtml/makeWebpage$ ./makeWebpage18BEv2.py
Traceback (most recent call last):
File "./makeWebpage18BEv2.py", line 27, in <module>
pyFiles = [makeCheckboxesInlineV1, dropdownboxesInlineV1, createhtmlTableInlineV1, makeRBsInlineV2, readLinesTextboxesInlineV3, makeThankyouPHPInlineV1]
NameError: name 'makeCheckboxesInlineV1' is not defined
pedro@pedro-newssd:~/myHWpageSummer2019/textTohtml/makeWebpage$

I also tried

pyFiles = [makeCheckboxesInlineV1(), dropdownboxesInlineV1(), createhtmlTableInlineV1(), makeRBsInlineV2(), readLinesTextboxesInlineV3(), makeThankyouPHPInlineV1()]
Reply
#4
What I have not seen before: You're modifying the sys.path list.
Don't do this. Put all your python files into a directory and put an empty __init__.py file into this directory.
Then you can do:

import directory_name.module_name
The module_name is in this case the python file, you want to access in this directory.
The .py extension is not used. Please read more about the import system, before you try
to invent your own stuff.


If you put the names (of the functions) in the list, the functions have to be imported or defined.

def function1():
    return 42

def function2():
    return 1337

# then you can create the list, with the function objects inside.
my_funcs = [function1, function2]

# iteratring over functions, print the name, call it

for func in my_funcs:
    name = func.__name__
    print('Calling function:', name)
    result = func()
    print('Result:', result)
If you put my_funcs from line 8 to line 1, you'll get a NameError. The function is not defined yet.
You can also import functions from other modules and put them afterwards in a list.
But the order is always: Assign an object to a name, then you can access with name the object.

You should refactor the names and the places.
For example you can make a module called make.
All functions inside the make module, does not contain the name make.

You should have something like this:
import make

# functions from make
make.br()
make.a()
make.img()
make.table()
make.tr()
make.td()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Here is how to store python code in a bash variable, run the python code from bash and store the output of the python code in another bash variable. THIS IS BASH CODE containing an inline block of python
#!/bin/bash

read -r -d '' variable <<EOF
import sys
def hi():
    print("hello world")
hi()
EOF

result=`python3 -c "${variable}"`

echo $result $result $result
My output
Output:
hello world hello world hello world
Reply
#6
This is called here-doc in shell scripting.
I don't like to mix languages in one source-file.

Just make everything with Python.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
Thanks for the replies. I know this must actually be quite simple to do. I just do not know how. I think I am not explaining myself clearly.

An example:

I have another loooong Python program. "all_steps_in_1_Email.py", nearly 1000 lines. It gets emails from students, downloads them, saves them, marks their answers, writes the results to my timetable, calculates % scores, attendance, writes everything to Excel, formats the Excel file. It has 7 functions. I need all of them in sequence. There is no need to choose any particular function. So I just run all_steps_in_1_Email.py in bash. Works well for hundreds of students. Does my marking and inserts the results to my timetable in about 10 seconds!

Now I want this little program, makeWebpage.py, to make my homework webpage each week.

@DeaD_EyE

I modify the sys.path so Python can find my modules easily. That works great in a Python shell like Idle. In fact, all my code works well in the Python shell. It does just what I want it to do.

Now I want to run makeWebpage.py in a bash terminal. The only difference to all_steps_in_1_Email.py is, I will not need all the functions every time. I need to choose which one to use.

I make all my little functions available, but I may not need all of them.

How can I call the 1 or 2 I want? How to assign just 1 or 2 to myString in bash?

Someone advised to use argparse, but I can't understand exactly how to implement it.

At the moment, I do it like this:

1. Use 1 of my functions in bash, say makeTextboxes.py Save the output string as a text file called paragraph.
2. Run a simpler version of makeWebpage.py This takes my basic webpage, reads it in with readlines(), then changes the title of the webpage, the week number, the css file, the maximum score, the switch-off time, inserts "paragraph" at line 32, a few other things and saves the file as, for example, week10.html

All I want is to integrate 1 and 2, but 1 needs a choice of function in the bash shell.

I tried putting the functions in a list and also in a dictionary:

pyFiles = [makeHTMLCBs(), makeDropdownboxes(), makeHTMLtable(), makeHTML_RBs(), readLinesmakeTBs(), makeThankyouPHP()]
pyFiles = {'0': makeHTMLCBs(), '1': makeDropdownboxes(), '2': makeHTMLtable(), '3': makeHTML_RBs(), '4': readLinesmakeTBs(), '5': makeThankyouPHP()}
Either way, at runtime, Python immediately wants to execute the first function in the list or dictionary. I don't get a chance to choose say, myString = pyFiles[3]
Reply
#8
This is how I did it!

choices = ['checkboxes', 'dropdownboxes', 'htmltable', 'radiobuttons', 'textboxes', 'makethankyouphp']
pathToFile = '/home/pedro/myHWpageSummer2019/18BE/html/'
pathToParagraph = '/home/pedro/myHWpageSummer2019/textTohtml/paragraph/paragraph'
file = '18BEblanko.html'

while True:
    print('This is to make the paragraph string, this weeks content html.')
    carryon = input('Enter y to continue, enter nothing to stop. ')
    if not carryon == 'y':
        break
    print('What do you want first? The choices are: ')    
    for choice in choices:
        print(choice)    
    myChoice = input('What do you want? Enter your choice. ')
    while myChoice not in choices:
        for choice in choices:
            print(choice)
        myChoice = input('What do you want? Enter your choice. ')
            
    if myChoice == 'checkboxes':
        myString = makeHTMLCBs()
    elif myChoice == 'dropdownboxes':
        myString = makeDropdownboxes()
    elif myChoice == 'htmltable':
        myString = makeHTMLtable()
    elif myChoice == 'radiobuttons':
        myString = makeHTML_RBs()
    elif myChoice == 'textboxes':
        myString = readLinesmakeTBs()
    elif myChoice == 'makethankyouphp':
        myString = makeThankyouPHP()
Works ok, I get what I need.
Reply
#9
There is a nice library, pythondialog that I use sometimes for such terminal dialogs. The example on their main page is very similar to the above code. It works very well in a bash terminal. The nice thing is that it works also very well over ssh, you can use it on a remote server and have a GUI feeling in your terminal.
Reply
#10
(Apr-19-2019, 09:39 PM)Pedroski55 Wrote: I tried putting the functions in a list and also in a dictionary:

pyFiles = [makeHTMLCBs(), makeDropdownboxes(), makeHTMLtable(), makeHTML_RBs(), readLinesmakeTBs(), makeThankyouPHP()]
pyFiles = {'0': makeHTMLCBs(), '1': makeDropdownboxes(), '2': makeHTMLtable(), '3': makeHTML_RBs(), '4': readLinesmakeTBs(), '5': makeThankyouPHP()}
Either way, at runtime, Python immediately wants to execute the first function in the list or dictionary. I don't get a chance to choose say, myString = pyFiles[3]

Python documentation: How do I use strings to call functions/methods?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 423 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  How to see the date of installation of python modules. newbieAuggie2019 4 1,643 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Python modules for accessing the configuration of relevant paths Imago 1 1,386 May-07-2022, 07:28 PM
Last Post: Larz60+
  Call a bash script from within a Python programme Pedroski55 6 2,477 Dec-06-2021, 01:53 PM
Last Post: DeaD_EyE
  Showing and saving the output of a python file run through bash Rim 3 2,481 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Python modules to extract data from a graph? bigmit37 5 22,459 Apr-09-2021, 02:15 PM
Last Post: TysonL
  Where to add own python modules in WinPython? HinterhaeltigesSchlaengelchen 1 2,295 Jan-21-2021, 07:45 PM
Last Post: snippsat
  Including modules in Python using sys.path.append JoeDainton123 1 2,917 Aug-24-2020, 04:51 AM
Last Post: millpond
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,942 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 3,064 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020