Python Forum
Is it possible to update a CSS file from Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to update a CSS file from Python
#1
Hello!

I have a main.scss file (I'm using SASS) and a Jupyter Notebook (.ipynb) in the same folder. I want to update only ONE line of code in my main.scss file but I want to do it from my jupyter notebook's python code. Is this possible?

To illustrate, in my main.scss file I have this at the top:
$customer_color_1: #cb2324;

Using python, I ONLY want to update that $customer_color_1 variable to become as follows:
$customer_color_1: #005f9f;

Is this possible??? If so, how? Thank you for any help!!

-AL
Reply
#2
css is just a text file!

Assuming: $customer_color_1: #cb2324; only appears 1 time, very easy.
Leave out the $, I think that will complicate matters! I think it is a reserved symbol.

myfile = '/path/to/mycss_files/my_css_file.css')
with open('path2mycssfile') as mycss:
    mystring = mycss.read()

newstring = mystring.replace('customer_color_1: #cb2324', 'customer_color_1: #005f9f')

with open('path2mycssfile', 'w') as mycss:
    mycss.write(newstring)
Otherwise, if you know the line number, also very easy.

linenr = 1
with open('path2mycssfile') as mycss:
    mylist = mycss.readlines()

# list numbering starts at 0
# line 1 is mylist[0]
my_line = linenr - 1 
the_line = mylist[my_line]
the_new_line = the_line.replace('customer_color_1: #cb2324', 'customer_color_1: #005f9f')
mylist[my_line] = the_new_line
mystring = ''.join(mylist)
with open('path2mycssfile', 'w') as mycss:
    mycss.write(mystring)
If the text you want to replace is present on more than 1 line, find all the lines with that text, then display them with line number, choose the line you want and replace.
bigAL_python likes this post
Reply
#3
(Apr-17-2022, 10:07 AM)Pedroski55 Wrote: css is just a text file!.

While this is true, the structure of it does have meaning. It would be better to use a library because

- it saves you having to write and maintain code that is likely not the thing you want to care about when writing your application,
- since a library is focused on a particular task, they'll do a better job than something hand rolled (better tested, catches errors, etc.),
bigAL_python and buran like this post
Reply
#4
One other thought - do you want your browser reload the css file after the change is made? If so, your next challenge will be to force that, and make sure it is not read from cache.
bigAL_python likes this post
Reply
#5
Very helpful, thanks so much guys!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,071 Jan-21-2023, 11:33 PM
Last Post: SamLiu
  value null when update in json file 3lnyn0 6 3,283 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,495 Oct-21-2021, 03:29 AM
Last Post: buran
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,695 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Update Python Files Harshil 3 2,275 Aug-28-2020, 10:52 AM
Last Post: Harshil
  Config file update Olivier74 0 1,483 Aug-18-2020, 03:36 PM
Last Post: Olivier74
  update txt file but keep a specific line 3Pinter 2 2,088 Dec-16-2019, 07:54 AM
Last Post: 3Pinter
  Update any file Evil_Patrick 1 1,438 Sep-24-2019, 04:26 PM
Last Post: Larz60+
  XML update using python sathiyarajmca 1 2,102 Jun-18-2019, 06:11 PM
Last Post: Larz60+
  Python csv compare two file, update value if two value is matched kinojom 1 2,559 Apr-17-2019, 10:36 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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