LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-19-2019, 01:29 AM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
Run a python module in bash


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 in stackoverflow 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:

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

Quote:
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:

Quote:
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

Quote:
myString = readLinesTextboxesInlineV3()
in bash, I cannot write

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

Quote:
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

Quote:
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:

Quote:
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?
 
Old 04-19-2019, 09:58 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
You can run a Python module with the -m option, for example:
Code:
$ curl somewebsite.htm | python -m json.tool
If I understand you correctly, you are writing an interactive front-end to a bunch of Python-based web utilities that you developed yourself. My first question is: Why does the front-end have to be a bash script, and not a Python program?

If it really does have to be a Bash script, why do you need to get the module name using a Python program? Why don't you use shell commands:
Code:
#!/bin/bash
echo -e "Enter module name: \c"
read MODULE
python -m $MODULE
I am not sure what you mean by "assign myString to any 1 of the modules". Is myString a variable in each module? You seem to be wanting to pass some data from a shell script into a Python module. I am sure there are several ways to do that, depending on what exactly you want to achieve.
 
Old 04-20-2019, 09:23 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Not sure that you should do it this way, but to answer the concept.

Code:
myvar=$(python - <<< "print ('Hello there.')")
echo "$myvar"
Hello there.
Code:
bash_var=$(python - << EOF
import time
for i in range(0, 10):
    print (i)
EOF
)

echo "$bash_var"
0
1
2
3
4
5
6
7
8
9
 
Old 04-20-2019, 10:15 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Sorry, no need to import time in that last one. I was thinking of something that I did not do.

Here, this imports something and uses it.

Code:
bash_var=$(python - << EOF
import time
log = []
for i in range(0, 5):
    log.append(['hello'])
    print ('Wating')
    time.sleep(.1)
    print(log)
EOF
)

echo "$bash_var"
 
Old 04-20-2019, 06:48 PM   #5
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thank you for the replies, I am grateful.

I think I am not explaining myself clearly.

I use Ubuntu and Idle to develop my little python programs. They save me a lot of time. I run the programs in bash. That's how bash comes into this. I have tried all this in Idle and it all works fine. Just when I try to run it in bash, I can't choose which function to run.

In python, it is necessary to import modules. That makes various functions available.

Simple example:
Quote:
import os
This alone does nothing, but makes what I think are called 'methods' available.

After importing os, I can, for example do this:

Quote:
photos = os.listdir(pathToPhotos)
Now photos is a list of all the photo names. I can iterate through the list and do things with the photos, in my case put them in an excel file next to the student with that name.

The crux is here: .listdir()

I can import my functions into makeWebpage.py but I don't know how to make methods available using .mymethod() notation.

makeCheckboxesInlineV1.py is a little routine, module, to take a text file and "htmlify" it.

Here is a basic checkbox html:
Quote:
basicCB = '<input type="checkbox" name="X" id="X" value="WORD"> <label for="X"> WORD </label> <br> \n'
I can call basicCB any number of times in a loop and replace X and WORD with a number and a word from a text file list, put it all together and the html is done and saved as a text file. 5 seconds!

Another example, from makeCheckboxesInlineV1.py I import the function makeHTMLCBs() (at import time the brackets are not needed):

Quote:
from makeCheckboxesInlineV1 import makeHTMLCBs
I run makeWebpage.py in bash. First it imports all my little html-makers. I want to choose which 1 to run and assign to myString, so I put them in a list of functions (no ' '):

Quote:
pyFiles = [makeHTMLCBs(), makeDropdownboxes(), makeHTMLtable(), makeHTML_RBs(), readLinesmakeTBs(), makeThankyouPHP()]
but as soon as the Python interpreter comes across this, it immediately starts the function makeHTMLCBs() I cannot, like I can in Idle, assign myString = pyFiles[3]

If I put this in makeWebpage.py

Quote:
pyFiles = ['makeHTMLCBs', 'makeDropdownboxes', 'makeHTMLtable', 'makeHTML_RBs', 'readLinesmakeTBs', 'makeThankyouPHP']
This is just a list of strings and does nothing if I assign myString = pyFiles[3], well, it's just a string, not a function.

I want to choose which function from my functions I run. That seems difficult.

So, what I think I need is to import my html-maker files and make the individual methods available using .mymethod() notation like this:

myString = makeCheckboxesInlineV1.makeHTMLCBs()

But I don't know how to do this. I don't know how to give myself that choice in bash. I know many python modules do this.

At the moment I:

1. run 1 or more of my little html-makers, save all in a text file,
2. then run a simpler version of makeWebpage.py which calls the basic template and inserts the new html and various other details. Webpage done!

I just want to intergrate 1. and 2.
 
Old 04-20-2019, 08:07 PM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
As you noticed yourself, this calls a bunch of functions and puts the result in the array:
Code:
pyFiles = [makeHTMLCBs(), makeDropdownboxes(), makeHTMLtable(), makeHTML_RBs(), readLinesmakeTBs(), makeThankyouPHP()]
Remove the parentheses to obtain a list of functions:
Code:
pyFiles = [makeHTMLCBs, makeDropdownboxe, makeHTMLtabl, makeHTML_RBs, readLinesmakeTB, makeThankyouPH]
You can then make a call to function number 2, for example:
Code:
pyFiles[2]()
which is nothing else than a call to makeHTMLtable().

Now all this is Python. I still don’t quite understand how you plan to integrate this with Bash.

(I did not test this and refuse responsibility if my code blows up your computer )

Last edited by berndbausch; 04-20-2019 at 08:09 PM.
 
Old 04-21-2019, 02:55 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
would be nice to give some more sample/simplified code you already written. Hard to understand based on the 4 lines you posted.

If I understand well you mixed python and bash code and you want to use the same variables in both languages which is more or less impossible (actually it is not really impossible, but this is the hard way).
 
Old 04-21-2019, 08:52 PM   #8
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Sorry if I have bothered you.

I am an idiot, who has no training in Python and very little self-acquired knowledge. Also, sometimes you need to walk away from a problem to find a solution. I go to bed early and wake before dawn, which is a good time for thinking. Found the solution this morning!

I first import my modules (only learnt to do this 3 days ago!):

Code:
# import the html-makers

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
then give myself options:
Code:
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()
The above makes any choice of module available repeatedly. In reality, I will only ever use 1 or 2 in any given week. The rest of my little webpage is all the same except the week number, switch-on and switch-off dates.

I tried this just now, I ran it in bash! It does what I want! (After I got rid of all the indentation errors!)

Thank you all very much for your advice!

Last edited by Pedroski; 04-21-2019 at 09:18 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why don't python scripts run unless I run them with explicit invocations of python? RandomTroll Linux - Newbie 23 10-06-2016 01:29 PM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
configure: error: The xdg python module is required (pyxdg or python-xdg) Sargalus Linux - Software 7 03-24-2010 07:34 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration