Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store f-string with function calls in a file

I'm storing f-strings with function calls in a separate file (with lots of variables).

I am writing a script that has hundreds of variables which are then loaded into an HTML table. Some of the contents in the HTML table require function calls.

This works:

def add_one(a):
    return a + 1

a = 1
s = f"A is {a} and next comes {add_one(a)}"
print(s)

When I store s in a file, I can use **locals() to format it and it works when I store variables in s.txt.

Contents of s.txt:

A is {a}

Contents of script that works:

a = 1
print(open('s.txt').read().format(**locals()))

However, when I try to call functions, it does not work:

Contents of s.txt:

A is {a} and next comes {add_one(a)}

Contents of script that does not work:

def add_one(a):
    return a + 1

a = 1
print(open('s.txt').read().format(**locals()))

What can I do to make it work (given my actual case is hundreds of function calls and not this simple 2 variable example)?

In this example it should result in A is 1 and next comes 2.

like image 835
Vasco d'Orey Avatar asked Jan 27 '26 08:01

Vasco d'Orey


1 Answers

You might want to consider using a templating language rather than f-strings if you have a complex HTML table with hundreds of variables. e.g. Jinja2.

For simplicity I've stored the a value in a dictionary as this then simplifies passing it to the Jinja2 render and also converting it to JSON for storing it in a file.

Here is your example using Jinja2 templates and storing the data to a json file:

import json
from pathlib import Path

import jinja2

json_file = Path('/tmp/test_store.json')

jinja_env = jinja2.Environment()

# Set variable values
values = {'a': 3}

# Save to json file
json_file.write_text(json.dumps(values))

# Read from json file to dictionary with new variable name
read_values = json.loads(json_file.read_text())


def add_one(a):
    return a + 1


# Add custom filter to jinja environment
jinja_env.filters['add_one'] = add_one
# Define template
template = jinja_env.from_string("A is {{a}} and next comes {{a | add_one}}")
# Print rendered template
print(template.render(read_values))

This gave the output of:

A is 3 and next comes 4

The JSON file is the following:

{"a": 3}
like image 126
ukBaz Avatar answered Jan 28 '26 23:01

ukBaz